With ES2016 we now have promises and that's great. Unfortunately the functionality is very minimalistic and there is nothing like the series or waterfall as available in the async package. If there a package providing this functionality for promises or how do people typically deal with those use cases?
Asked
Active
Viewed 100 times
1
-
Are you asking for a promise library? – evolutionxbox Feb 13 '17 at 12:28
-
3That's what `.then` is for – Paul Feb 13 '17 at 12:29
-
@evolutionxbox if that's the way to go: yes – doberkofler Feb 13 '17 at 12:30
-
http://bluebirdjs.com/ ? – trincot Feb 13 '17 at 12:30
-
@trincot I understood bluebird.js more as a complete alternative to native promises. Can it also be used on top of native promises or are there alternatives that just enhance the native promises? – doberkofler Feb 13 '17 at 12:39
-
@paul so you would implement a waterfall execution of an array of function returning promises manually using .then()? – doberkofler Feb 13 '17 at 12:41
-
1@materialdreams you can use `.reduce` to serially execute an array of functions returning promises – Alnitak Feb 13 '17 at 12:43
-
Promises that adhere to the Promises/A+ specs can inter-operate, so using bluebird together with ES6 Promise objects will work. – trincot Feb 13 '17 at 12:53
-
Bluebird `mapSeries`. It can successfully coexist with native promises, though you may want to stick to Bb whenever possible because it offers good performance and a lot of good things. Depending on the situation, it may be a good idea to replace global `Promise` with Bb. – Estus Flask Feb 13 '17 at 16:53
-
Possible duplicate of [ES6 Promises - something like async.each?](http://stackoverflow.com/questions/32028552/es6-promises-something-like-async-each) – jib Feb 14 '17 at 02:47
2 Answers
5
To serially execute an array of functions returning promises you can use Array.prototype.reduce
:
let final = functions.reduce((prev, f) => prev.then(f), Promise.resolve());
The "initial" argument Promise.resolve()
is there to seed the chain of promises, since otherwise (if passed an array containing only a single function) the .reduce
callback never gets called.

Alnitak
- 334,560
- 70
- 407
- 495
1
Most of this functionality already exists (or will exist) in the language:
- Run a bunch of actions simultaneously and get a Promise for an array of results:
Promise.all()
- Run a bunch of actions and get the Promise for the first that resolves/rejects:
Promise.race()
- Run a bunch of Promises serially: Use
reduce()
like the other answer mentions, or use the async iteration protocol.

Graham
- 7,431
- 18
- 59
- 84

Madara's Ghost
- 172,118
- 50
- 264
- 308