Using Promise.race to Resolve the Fastest Promise in Javascript


Resolve as soon as one of the promises in the iterable resolves.

Source Code

let promise1 = new Promise((resolve, reject) => setTimeout(resolve, 500, 'one'));
let promise2 = new Promise((resolve, reject) => setTimeout(resolve, 100, 'two'));

Promise.race([promise1, promise2]).then(value => {
  console.log(value); // "two"
});
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments