question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Implement Promise.series

See original GitHub issue

When you have the list of values to be passed to the same function sequentially, so that the next call is only made only when the promise returned by the previous call is resolved, you either have to “manually” chain promises or use Promise.reduce, which both looks a bit ugly and also not so obvious.

It could be nice to have something like this:

Promise.series = function (arr, iteratorAsync) {
    return Promise.reduce(arr, function(memo, item) {
        return iteratorAsync(item);
    }, 0);
}

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Comments:21 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
ericclemmonscommented, Mar 10, 2014

Thank God I found this issue. I was under the impression that Promise.all was iterative. (I was .push’ing promises onto an Array, expecting sequential execution).

2reactions
petkaantonovcommented, Mar 7, 2014

The reason for popularity of this suggestion is misunderstanding of promises and thinking of Promise.all as some kind of “parallel” which needs a sequential equivalent.

But Promise.all doesn’t “parallelize” promises or anything, it does nothing to the input promises - it is simply just telling you whether the promises you already are controlling from somewhere else are completed or not.

So because promises are not being controlled from the collection methods, Promise.all cannot have a .sequence equivalent - a Promise.sequence would always need a tailored “iteratorAsync” function to go with it - just like reduce so no net savings of lines of code are gained .

Read more comments on GitHub >

github_iconTop Results From Across the Web

Implementing Promise.series as alternative to Promise.all
If you truly want to run your promises in series, you should adjust your function and pass in an array of functions that...
Read more >
Promise - JavaScript - MDN Web Docs
The Promise object represents the eventual completion (or failure) of an asynchronous operation and its resulting value.
Read more >
How To Create Your Own Implementation Of JavaScript ...
How To Create Your Own Implementation Of JavaScript Promises. Watch later. Share. Copy link. Info. Shopping. Tap to unmute.
Read more >
Implement Promise.all - Rajat Explains
This blog is about solving an interview question to implement Promise.all. ... This blog post is a part of the frontend interview series....
Read more >
Implementing - Promises
Our goal here is to implement promise.done(onFulfilled, onRejected) such that: only one of onFulfilled or onRejected is called; it is only called once ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found