Skip to content

Repository files navigation

@codewell/chain

JavaScript function for chaining functions

Description

The chaining function takes an arbitrary number of arguments. The first argument is of any type, the rest should all be functions sychronous or asychronous. Chain automatically returns a promise if one or more arguments is an async function. The first parameter is passed as input to the second argument (a funciton) which in turn is passed its return value to the next funciton and so on. E.g.

chain(0,(n)=>n+1,// => 0 + 1(n)=>n+1// => 1 + 1);// => 2

A requirement is therefore that all functions passed as arguments to the chain function has a return value (functions included).

Installation

npm install @codewell/chain

Basic usage

importchainfrom'@codewell/chain';constaddThree=n=>n+3;constmultiplyByFive=m=>m*5;chain(1,addThree,multiplyByFive);// => 20
// Async exampleimportchainfrom'@codewell/chain';constaddThreeAsync=(n)=>newPromise((resolve,reject)=>{resolve(n+3);})constmultiplyByFiveAsync=(m)=>newPromise((resolve,reject)=>{resolve(m*5);})chain(1,addThreeAsync,multiplyByFiveAsync).then(result=>{// Handle result somehow...})

About

Chaning function in JavaScript

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages