Update chain-async-functions.md
Improved example and formatting
This commit is contained in:
@ -3,13 +3,12 @@
|
|||||||
Loop through an array of functions containing asynchronous events, calling `next` when each asynchronous event has completed.
|
Loop through an array of functions containing asynchronous events, calling `next` when each asynchronous event has completed.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const chainAsync = fns => {
|
const chainAsync = fns => { let curr = 0; const next = () => fns[curr++](next); next(); }
|
||||||
let curr = 0; const next = () => fns[curr++](next); next()
|
/*
|
||||||
}
|
|
||||||
chainAsync([
|
chainAsync([
|
||||||
next => { console.log('This happens at 0 seconds'); setTimeout(next, 1000) },
|
next => { console.log('0 seconds'); setTimeout(next, 1000); },
|
||||||
next => { console.log('This happens at 1 second'); setTimeout(next, 1000) },
|
next => { console.log('1 second'); setTimeout(next, 1000); },
|
||||||
next => { console.log('This happens at 2 seconds'); setTimeout(next, 1000) },
|
next => { console.log('2 seconds');}
|
||||||
next => { console.log('Done at 3 seconds!') }
|
|
||||||
])
|
])
|
||||||
|
*/
|
||||||
```
|
```
|
||||||
|
|||||||
Reference in New Issue
Block a user