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:

Exploring The Aha Stack: Astro, Htmx, Alpine — A Complete Tutorial With A Demo Project And Comparison To Other Stacks

Exploring the AHA stack: Tutorial, demo, and comparison

The AHA stack — Astro, htmx, and Alpine — is a solid web development stack for smaller apps that emphasize frontend speed and SEO.

Oyinkansola Awosan
May 3, 2024 â‹… 13 min read
Comparing Hattip Vs Express Js For Modern Application Development

Comparing Hattip vs. Express.js for modern app development

Explore what Hattip is, how it works, its benefits and key features, and the differences between Hattip and Express.js.

Antonello Zanini
May 2, 2024 â‹… 8 min read
Using React Shepherd To Build A Site Tour

Using React Shepherd to build a site tour

React Shepherd stands out as a site tour library due to its elegant UI and out-of-the-box, easy-to-use React Context implementation.

Onuorah Bonaventure
May 1, 2024 â‹… 14 min read
A Guide To Cookies In Next Js

A guide to cookies in Next.js

Cookies are crucial to web development. This article will explore how to handle cookies in your Next.js applications.

Georgey V B
Apr 30, 2024 â‹… 10 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