Added async function chain

This commit is contained in:
Robin Thomas
2017-12-12 10:24:42 -06:00
parent e93e618283
commit e0fcf9fe05

View File

@ -0,0 +1,15 @@
### Chain asynchronous functions
Loop through an array of functions containing asynchronous events, calling `next` when each asynchronous event has completed.
```js
const chainAsync = fns => {
let curr = 0; const next = () => fns[curr++](next); next()
}
chainAsync([
next => { console.log('This happens at 0 seconds'); setTimeout(next, 1000) },
next => { console.log('This happens at 1 second'); setTimeout(next, 1000) },
next => { console.log('This happens at 2 seconds'); setTimeout(next, 1000) },
next => { console.log('Done at 3 seconds!') }
])
```