DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’» is a community of 963,274 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Create account Log in
florent giraud
florent giraud

Posted on

[REMINDER-5] Promise.race()

Promise.race() runs as soon as one of the promises you pass to it resolves, and it runs the attached callback just once with the result of the first promise resolved.

example:

const promiseOne = new Promise((resolve, reject) => {
  setTimeout(resolve, 500, 'one')
})
const promiseTwo = new Promise((resolve, reject) => {
  setTimeout(resolve, 100, 'two')
})

Promise.race([promiseOne, promiseTwo]).then(result => {
  console.log(result) // 'two'
})
Enter fullscreen mode Exit fullscreen mode

More important here is to know a use case. I don't have really use case for that.. But in case you want to execute a function as soons as possible or maybe if you are implementing a rendering and call an api at the same time. If the rendering fail at some point. The api call will stop too.

That's it for this reminder

Top comments (0)

One of the best series of the year

How web browsers work (with illustrations)

Seven part series β€” highly bookmarkable πŸ˜…