Node.js Promise Race Link to heading

The Promise.race() method takes an iterable of promises and returns the first promise to resolve.

const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));

const a = new Promise(async (resolve) => {
  await sleep(1000);
  resolve("a");
});

const b = new Promise(async (resolve) => {
  await sleep(2000);
  resolve("b");
});

return Promise.race([a, b]);
// => "a"