DEV Community

Cover image for Make your promises faster | 1 Minute Tip
Charlie Say
Charlie Say

Posted on • Updated on

Make your promises faster | 1 Minute Tip

As a javascript developer, you may use promises ALL the time. If you use them you know about the await keyword. Used to wait for a promise to resolve.

But you can make these quicker, say you are interacting with two separate endpoints for example :

let endpointApiOne = await getWeatherEndpoint();
let endpointApiTwo = await getUserEndpoint();

While this works, there is a slight performance issue with this. These two different calls will be performed synchronously.

But do not fear! There is a way to allow these to run asynchronously the magic Promise.allSettled

let [endpointApiOne, endpointApiTwo] =
await Promise.allSettled([getWeatherEndpoint(), getUserEndpoint()])

This will return an array of fulfilled ( or rejected ) promises instead.

For more reading have a look at the MDN Docs
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/allSettled

That is the first of 1 minute tips!

Top comments (0)