https://github.com/codewell/chain
Chaning function in JavaScript
https://github.com/codewell/chain
Last synced: 6 days ago
JSON representation
Chaning function in JavaScript
- Host: GitHub
- URL: https://github.com/codewell/chain
- Owner: codewell
- License: mit
- Created: 2019-09-12T13:58:35.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2023-03-04T04:43:11.000Z (over 3 years ago)
- Last Synced: 2024-10-16T11:04:15.044Z (over 1 year ago)
- Language: JavaScript
- Size: 363 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# @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.
```JavaScript
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
```JavaScript
import chain from '@codewell/chain';
const addThree = n => n + 3;
const multiplyByFive = m => m * 5;
chain(1, addThree, multiplyByFive);
// => 20
```
```JavaScript
// Async example
import chain from '@codewell/chain';
const addThreeAsync = (n) => new Promise((resolve, reject) => {
resolve(n + 3);
})
const multiplyByFiveAsync = (m) => new Promise((resolve, reject) => {
resolve(m * 5);
})
chain(1, addThreeAsync, multiplyByFiveAsync)
.then(result => {
// Handle result somehow...
})
```