Merge pull request #889 from RobertAKARobin/feature/chainasync-updates

[FEATURE] chainAsync update: last function shouldn't get 'next' as argument
This commit is contained in:
Angelos Chalaris
2018-12-13 00:03:10 +02:00
committed by GitHub
2 changed files with 27 additions and 7 deletions

View File

@ -7,7 +7,11 @@ Loop through an array of functions containing asynchronous events, calling `next
```js
const chainAsync = fns => {
let curr = 0;
const next = () => fns[curr++](next);
const last = fns[fns.length - 1];
const next = () => {
const fn = fns[curr++]
fn === last ? fn() : fn(next);
}
next();
};
```
@ -20,6 +24,10 @@ chainAsync([
},
next => {
console.log('1 second');
setTimeout(next, 1000);
},
() => {
console.log('2 second');
}
]);
```