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!

Image of Datadog

The Essential Toolkit for Front-end Developers

Take a user-centric approach to front-end monitoring that evolves alongside increasingly complex frameworks and single-page applications.

Get The Kit

Top comments (0)

Billboard image

Try REST API Generation for Snowflake

DevOps for Private APIs. Automate the building, securing, and documenting of internal/private REST APIs with built-in enterprise security on bare-metal, VMs, or containers.

  • Auto-generated live APIs mapped from Snowflake database schema
  • Interactive Swagger API documentation
  • Scripting engine to customize your API
  • Built-in role-based access control

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay