aweber-banner
Promises helper functions

Promises helper functions

In this post, we will learn about some useful promises helper functions and their uses with examples.

Promises helper functions

Promise all

Running Promises in Parallel instead of running these promises in sequence,

we can actually run them in parallel, so all at the same time. And so then we can save valuable loading time

Also, note that if one of the promises rejects, then the whole promise.all actually rejects as well.

We can say that promise.all short circuits when one promise rejects.

so whenever you have a situation in which you need to do multiple asynchronous operations at the same time, and operations that don't depend on one another, then you should always, always run them in parallel.

just like we did here using promise.all.

 const getData = async function (c1, c2, c3) {
    try {
    
      const data = await Promise.all([
      ............
      ]);
      
    } catch (err) {
      console.error(err);
    }
  };
  getData ();

Also read, Promises in JavaScript

Promise.race

Promise.race, just like all other combinators, receives an array of promises and it also returns a promise.

Now this promise returned by Promise.race is settled as soon as one of the input promises settles.

And remember that settled simply means that a value is available, but it doesn't matter

if the promise got rejected or fulfilled.

So in Promis.race, basically the first settled promise wins the race.

In Promised.race, we only get one result and not an array of the results of all the three.

Now a promise that gets rejected can actually also win the race, so we can say that Promise.race short circuits.

Promise.race is actually very useful to prevent against never ending promises or also very long running promises.

For example, if your user has a very bad internet connection, then a fetch requests in your application might take way too long to actually be useful and so we can create a special time out promise, which automatically rejects after a certain time has passed.

Promise.allSettled.

It is from ES2020 and it is actually a very simple one.

So it takes in an array of promises again, and it will simply return an array of all the settled promises, no matter if the promises got rejected or not.

So it's similar to Promise.all in regard that it also returns an array of all the results,

but the difference is that Promise.all will short circuit as soon as one promise rejects,

but Promise.allSettled, simply never short circuits, it will simply return all the results of all the promises.

Promise.any

Now Promise.any is even more modern,Its from ES2021

Promise.any takes in an array of multiple promises and this one will then return the first fulfilled promise and it will simply ignore rejected promises.

So basically Promise.any is very similar to Promise.race with the difference that rejected promises are ignored, so therefore the results of Promise.any is always gonna be a fulfilled promise, unless of course all of them reject.

Subscribe to our Newsletter

Stay up to date! Get all the latest posts delivered straight to your inbox.

If You Appreciate What We Do Here On TutsCoder, You Should Consider:

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Support Us

We are thankful for your never ending support.

Leave a Comment