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

v2.1.1

Published

function that make syncronous chain call pretty and effectively.

Downloads

22

Readme

ChainCallJs 2.0

Overview

ChaincallJs@2 is targeting below goals.

  1. Reduce complexity of the code
  2. Fast auto-completion
  3. Induce modularization of the code.

While this is a very simple library, it is suggesting stream-based programming to reduce complexity in a logical process.

For example, in Java, stream syntax simplifies the logical flow. We have similar structure in Promise. We have the new async/await model now, but Promise's .then() model helps to block the logical flows in the different boundary.

ChainCallJs@2 is trying to provide similar structure in the synchronous model.

Install (Or just copy the code in your repo!)

npm i chaincalljs@2

Motivation

Let's say we have a long logic. Most of the time, we will need to call functions and get the return values and pass some of it into the next function. No problem. But what it will be very easy to make a complex inter-relations between multiple function calls.


const a = funcA(param1, param2, param3);
const {b,c,d} = a;
const e = funcB(param1, param2, c, d);
const f = funcC(param2, b, e);
const {g, h} = f;
const i = funcD(g, h);

Yeah. I know. it is common. But it makes the code to read and analyze harder. What if we can limit blocks?

const a = funcA(param1, param2, param3);
let f;
{
    const {b,c,d} = a;
    const e = funcB(param1, param2, c, d);
    f = funcC(param2, b, e);
}
const {g, h} = f;
const i = funcD(g, h);

or we can make the block to the other function


const a = funcA(param1, param2, param3);
const doSomeWorks = (a)=>{
    const {b,c,d} = a;
    const e = funcB(param1, param2, c, d);
    return funcC(param2, b, e);
}
const f = doSomeWorks(a);
const {g, h} = f;
const i = funcD(g, h);

Both of the approach will be good. But what if I can do something like this?

Usage

const i = chain(funcA(param1, param2, param3))
    .then(({b,c,d})=>
        chain(funcB(param1, param2, c, d))
            .then((e)=>funcC(param2, b, e))
    )
    .then(({g, h})=>funcD(g, h))
    .value();

the flow above has specific block. Of course, param1, param2 is used in the multiple levels, but temporary return values are not overflowing.

The benefit of this flow is, you can clearly see the dependencies between flows without making unnecessary function logics which will require a lot of management.

the whole chancalljs logic is very simple.

export const chain = <T, S>(ret: T, store: S = {} as S) => {
	return {
		then: <U>(fn: (_ret: T, store: S) => U) => {
			return chain<U, S>(fn(ret, store), store);
		},
		value: () => {
			return ret;
		},
	};
};

but it is very powerful since it support type and enforce a clean coding standard.


const isWorkGroupDone = chain(['songhyeon', 'jun', 44, 'senior engineer'])
			.then(([first, last, age, job]) => ({
				name: `${first} ${last}`,
				ageGroup: getAgeGroup(age),
				jobType: getJobType(job),
			}))
			.then(({ name, ageGroup, jobType }) => {
				// if the values should be skipped the chain flow and should be used in the middle, make another chain flow.
				const store = {count: nameCards.length};
				return chain([name, jobType], store)
					.then(([name, jobType]) => nameCard(name, jobType))
					.then((card, store) =>( printNamecard(card), store.count++, card))
					.then((card, store) =>( printNamecard(card), store.count++, card))
					.then((card, store) =>( printNamecard(card), store.count++, card))
					.then((card, store) =>( printNamecard(card), store.count++, card))
					.then((_, store)=>nameCards.length === store.count && store.count === 4)
					.then(isNameCardPrinted =>(isNameCardPrinted && increaseWorkDone(ageGroup), ageGroup))
					.then(ageGroup=>numOfWorkDone[ageGroup] > 0)
					.value();
			})
			.value();

when you see the code, you can see the whole complex flow in one glance.

if above logic is written without chaincalljs, this would look like this:


const [first, last, age, job] = ['songhyeon', 'jun', 44, 'senior engineer'];

const name = `${first} ${last}`;

const ageGroup = getAgeGroup(age);
const jobType = getJobType(job);
const card = nameCard(name, jobType);

const store = {count: nameCards.length};

printNamecard(card);
printNamecard(card);
printNamecard(card);
printNamecard(card);

const isNameCardPrinted = nameCards.length === store.count && store.count === 4;
isNameCardPrinted && increaseWorkDone(ageGroup);

const isWorkGroupDone = numOfWorkDone[ageGroup] > 0;

It could be a preference of coding, but to me, chaincalljs solution looks more organized. On the contrary, the logic without chaincalljs tends to be a bit more scattered and hard to find the relationship between logics.

try/catch

Unlike Promise's .then() model, chaincalljs does not support .catch() function. It makes harder to infer return type of the function. I recommend you to just wrap it with try/catch to make the pattern more obvious.

const myFunc = ():D => {
    try{
        // since we don't know
        return chain(funcA(param1, param2, param3)) // return A type
            .then(funcB) // return B type 
            .then(funcC) // return C type
            .then(fundD) // return D type
            .value(); // will return D type if everything passes.

    }catch(e){
        console.error("process failed", e);
        return DEFAULT_RET as D; // return D type to keep the type consistent.
    }
}

Support

For support, questions, or to report issues related to the ChainCallJs, please use the GitHub Issues page of the Turborepo project.

License

The SVG Table Component is MIT License.