2021-11-08
2621
#vanilla javascript
Fernando Doglio
4429
Nov 8, 2021 â‹… 9 min read

JavaScript Promises: race, all, allSettled, and then

Fernando Doglio Technical Manager at Globant. Author of books and maker of software things. Find me online at fdoglio.com.

Recent posts:

Nx Adoption Guide: Overview, Examples, And Alternatives

Nx adoption guide: Overview, examples, and alternatives

Let’s explore Nx features, use cases, alternatives, and more to help you assess whether it’s the right tool for your needs.

Andrew Evans
Mar 28, 2024 â‹… 9 min read
Understanding Security In React Native Applications

Understanding security in React Native applications

Explore the various security threats facing React Native mobile applications and how to mitigate them.

Wisdom Ekpotu
Mar 27, 2024 â‹… 10 min read
Warp Adoption Guide: Overview, Examples, And Alternatives

warp adoption guide: Overview, examples, and alternatives

The warp web framework for Rust offers many enticing features. Let’s see when and why you should consider using warp in your projects.

Ukeje Goodness
Mar 26, 2024 â‹… 8 min read
Integrating Next Js And Signalr For Enhanced Real Time Web App Capabilities

Integrating Next.js and SignalR to build real-time web apps

In this tutorial, you’ll learn how to integrate Next.js and SignalR to build an enhanced real-time web application.

Clara Ekekenta
Mar 25, 2024 â‹… 8 min read
View all posts

3 Replies to "JavaScript Promises: race, all, allSettled, and then"

  1. Mozilla’s web documentation at https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all has an good workaround for the problem of rejection stopping Promise.all(). I have been using it without any issues so far:

    var p1 = new Promise((resolve, reject) => {
    setTimeout(() => resolve(‘p1_delayed_resolution’), 1000);
    });

    var p2 = new Promise((resolve, reject) => {
    reject(new Error(‘p2_immediate_rejection’));
    });

    Promise.all([
    p1.catch(error => { return error }),
    p2.catch(error => { return error }),
    ]).then(values => {
    console.log(values[0]) // “p1_delayed_resolution”
    console.log(values[1]) // “Error: p2_immediate_rejection”
    })

  2. Nice article, but one thing should be noted. You say:

    “By definition,Promise.all will run all your promises until one of the following conditions are met:”

    It’s not true. Promise.all doesn’t run anything. Promises are ALWAYS fired immediately, always. There are no methods to explicitly control Promise’s execution. Promise.all just listens their execution, but doesn’t do anything else.

Leave a Reply