Add run-promises-in-series

This commit is contained in:
Darren Scerri
2017-12-13 10:44:44 +01:00
parent 54969e66f3
commit 85709fccda
2 changed files with 21 additions and 1 deletions

View File

@ -0,0 +1,9 @@
### Run promises in series
Run an array of promises in series using `Array.reduce()` by creating a promise chain, where each promise returns the next promise when resolved.
```js
var series = ps => ps.reduce((p, next) => p.then(next), Promise.resolve());
// var delay = (d) => new Promise(r => setTimeout(r, d))
// series([() => delay(1000), () => delay(2000)]) -> executes each promise sequentially, taking a total of 3 seconds to complete
```