DEV Community

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

Posted on • Edited on

1

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!

SurveyJS custom survey software

Simplify data collection in your JS app with a fully integrated form management platform. Includes support for custom question types, skip logic, integrated CCS editor, PDF export, real-time analytics & more. Integrates with any backend system, giving you full control over your data and no user limits.

Learn more

Top comments (0)

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more