The Promise.race(iterable) method

The race() method of the Promise object takes an iterable object as the argument and returns a Promise that fulfills or rejects as soon as one of the promises in the iterable object is fulfilled or rejected, with the fulfillment value or reason from that Promise.

As the name suggests, the race() method is used to race between promises and see which one finishes first. Here is a code example that shows how to use the race() method:

var p1 = new Promise(function(resolve, reject){ setTimeout(function(){ resolve("Fulfillment Value 1"); }, 1000);});var p2 = new Promise(function(resolve, reject){ setTimeout(function(){resolve("fulfillment Value 2"); }, 2000);});var arr = [p1, p2];Promise.race(arr).then(function(value){ ...

Get Learn ECMAScript - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.