npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

chaincalljs

v1.0.1

Published

function that make syncronous chain call pretty and effectively by using a single promise.

Downloads

6

Readme

Chain Call helper (ChainCallJs)

Spoiler

If you want to see how to use, go to How to use section directly.

how to install

npm i chaincalljs

Why you need this module

For example, Let's say I want to write this code:

const arr = [1,2,3,4,5];
let ret = func1(arr);
ret = func2(ret);
ret = func3(ret);
console.log(ret);

as you see, we want to make the code shorter. How about like this:

const arr = [1,2,3,4,5];
const ret = func3(
    func2(
        func1(arr)));
console.log(ret);

even though the order of run is func1, func2, func3, the code show up backward. I don't like it. And also, it is using multiple stacks. and it will be inefficient for the memory management.

What if we use Promise?

const arr = [1,2,3,4,5];
new Promise(resolve=>resolve(arr))
.then(ret=>func1(ret))
.then(ret=>func2(ret))
.then(ret=>func3(ret))
.then(console.log);

Much better. If it is time-consuming jobs, it may be a good change because the callback console.log will be called when all the functions are finished. But if it is not? If they are pure syncronous functions and it will be finished quickly, and I want to do something after this function calls?

We may need to make this function as async function and make this promise await. But still the function will be changed to async function.

Complexity will be increased. And also, unnecessary Promise call will reduce your performance.

What if I can do like this?

const arr = [1,2,3,4,5];
const ret = chainCallNp(
    arr,
    func1,
    func2,
    func3
);
console.log(ret);

This is purely syncronous function calls, and easy to understand as well.

This module will help you to program like above and some of more extensive functionalities.

How to use

UseCase 1 - Pure Syncronous Chain Calls

const np = require('chaincalljs');
const ret = np.chainCallNp(
    [1,2,3,4,5],
    func1,
    func2,
    func3
);
console.log('chainCallNp:', ret);

UseCase 2 - async call with 1 Promise

const np = require('chaincalljs');
np.chainCall(
    [1,2,3,4,5],
    func1,
    func2,
    func3
).then(ret=>console.log('chainCall:', ret));

UseCase 3 - when you want to call callbacks for each call

If your original code is like this:

const arr = [1,2,3,4,5];
let ret = func1(arr);
callOther(ret, "first call");
ret = func2(ret);
callOther(ret, "second call");
ret = func3(ret);
console.log(ret);

as you see, after 1st and 2nd call, you want to call callOther function with the result. using chaincalljs module, you can call like this:

const np = require('chaincalljs');
const arr = [1,2,3,4,5];
const ret = np.chainCallNp(
    arr,
    np.hoc(func1, "first call", callOther),
    np.hoc(func2, "second call", callOther),
    func3
);
console.log(ret);

the hoc function will return an object that the chaincalljs module can understand, and do what you want to do.

Enjoy!