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)

In defense of the modern web

I expect I'll annoy everyone with this post: the anti-JavaScript crusaders, justly aghast at how much of the stuff we slather onto modern websites; the people arguing the web is a broken platform for interactive applications anyway and we should start over;

React users; the old guard with their artisanal JS and hand authored HTML; and Tom MacWright, someone I've admired from afar since I first became aware of his work on Mapbox many years ago. But I guess that's the price of having opinions.