Movatterモバイル変換


[0]ホーム

URL:


MDN Web Docs

Promise.race()

BaselineWidely available

ThePromise.race() static method takes an iterable of promises as input and returns a singlePromise. This returned promise settles with the eventual state of the first promise that settles.

Try it

const promise1 = new Promise((resolve, reject) => {  setTimeout(resolve, 500, "one");});const promise2 = new Promise((resolve, reject) => {  setTimeout(resolve, 100, "two");});Promise.race([promise1, promise2]).then((value) => {  console.log(value);  // Both resolve, but promise2 is faster});// Expected output: "two"

Syntax

js
Promise.race(iterable)

Parameters

iterable

Aniterable (such as anArray) of promises.

Return value

APromise thatasynchronously settles with the eventual state of the first promise in theiterable to settle. In other words, it fulfills if the first promise to settle is fulfilled, and rejects if the first promise to settle is rejected. The returned promise remains pending forever if theiterable passed is empty. If theiterable passed is non-empty but contains no pending promises, the returned promise is still asynchronously (instead of synchronously) settled.

Description

ThePromise.race() method is one of thepromise concurrency methods. It's useful when you want the first async task to complete, but do not care about its eventual state (i.e., it can either succeed or fail).

If the iterable contains one or more non-promise values and/or an already settled promise, thenPromise.race() will settle to the first of these values found in the iterable.

Examples

Using Promise.race()

This example shows howPromise.race() can be used to race several timers implemented withsetTimeout(). The timer with the shortest time always wins the race and becomes the resulting promise's state.

js
function sleep(time, value, state) {  return new Promise((resolve, reject) => {    setTimeout(() => {      if (state === "fulfill") {        resolve(value);      } else {        reject(new Error(value));      }    }, time);  });}const p1 = sleep(500, "one", "fulfill");const p2 = sleep(100, "two", "fulfill");Promise.race([p1, p2]).then((value) => {  console.log(value); // "two"  // Both fulfill, but p2 is faster});const p3 = sleep(100, "three", "fulfill");const p4 = sleep(500, "four", "reject");Promise.race([p3, p4]).then(  (value) => {    console.log(value); // "three"    // p3 is faster, so it fulfills  },  (error) => {    // Not called  },);const p5 = sleep(500, "five", "fulfill");const p6 = sleep(100, "six", "reject");Promise.race([p5, p6]).then(  (value) => {    // Not called  },  (error) => {    console.error(error.message); // "six"    // p6 is faster, so it rejects  },);

Asynchronicity of Promise.race

This following example demonstrates the asynchronicity ofPromise.race. Unlike other promise concurrency methods,Promise.race is always asynchronous: it never settles synchronously, even when theiterable is empty.

js
// Passing an array of promises that are already resolved,// to trigger Promise.race as soon as possibleconst resolvedPromisesArray = [Promise.resolve(33), Promise.resolve(44)];const p = Promise.race(resolvedPromisesArray);// Immediately logging the value of pconsole.log(p);// Using setTimeout, we can execute code after the stack is emptysetTimeout(() => {  console.log("the stack is now empty");  console.log(p);});// Logs, in order:// Promise { <state>: "pending" }// the stack is now empty// Promise { <state>: "fulfilled", <value>: 33 }

An empty iterable causes the returned promise to be forever pending:

js
const foreverPendingPromise = Promise.race([]);console.log(foreverPendingPromise);setTimeout(() => {  console.log("the stack is now empty");  console.log(foreverPendingPromise);});// Logs, in order:// Promise { <state>: "pending" }// the stack is now empty// Promise { <state>: "pending" }

If the iterable contains one or more non-promise value and/or an already settled promise, thenPromise.race will settle to the first of these values found in the array:

js
const foreverPendingPromise = Promise.race([]);const alreadyFulfilledProm = Promise.resolve(100);const arr = [foreverPendingPromise, alreadyFulfilledProm, "non-Promise value"];const arr2 = [foreverPendingPromise, "non-Promise value", Promise.resolve(100)];const p = Promise.race(arr);const p2 = Promise.race(arr2);console.log(p);console.log(p2);setTimeout(() => {  console.log("the stack is now empty");  console.log(p);  console.log(p2);});// Logs, in order:// Promise { <state>: "pending" }// Promise { <state>: "pending" }// the stack is now empty// Promise { <state>: "fulfilled", <value>: 100 }// Promise { <state>: "fulfilled", <value>: "non-Promise value" }

Using Promise.race() to implement request timeout

You can race a potentially long-lasting request with a timer that rejects, so that when the time limit has elapsed, the resulting promise automatically rejects.

js
const data = Promise.race([  fetch("/api"),  new Promise((resolve, reject) => {    // Reject after 5 seconds    setTimeout(() => reject(new Error("Request timed out")), 5000);  }),])  .then((res) => res.json())  .catch((err) => displayError(err));

If thedata promise fulfills, it will contain the data fetched from/api; otherwise, it will reject iffetch remains pending for 5 seconds and loses the race with thesetTimeout timer.

Using Promise.race() to detect the status of a promise

BecausePromise.race() resolves to the first non-pending promise in the iterable, we can check a promise's state, including if it's pending. This example is adapted frompromise-status-async.

js
function promiseState(promise) {  const pendingState = { status: "pending" };  return Promise.race([promise, pendingState]).then(    (value) =>      value === pendingState ? value : { status: "fulfilled", value },    (reason) => ({ status: "rejected", reason }),  );}

In this function, ifpromise is pending, the second value,pendingState, which is a non-promise, becomes the result of the race; otherwise, ifpromise is already settled, we may know its state through theonFulfilled andonRejected handlers. For example:

js
const p1 = new Promise((res) => setTimeout(() => res(100), 100));const p2 = new Promise((res) => setTimeout(() => res(200), 200));const p3 = new Promise((res, rej) =>  setTimeout(() => rej(new Error("failed")), 100),);async function getStates() {  console.log(await promiseState(p1));  console.log(await promiseState(p2));  console.log(await promiseState(p3));}console.log("Immediately after initiation:");getStates();setTimeout(() => {  console.log("After waiting for 100ms:");  getStates();}, 100);// Logs:// Immediately after initiation:// { status: 'pending' }// { status: 'pending' }// { status: 'pending' }// After waiting for 100ms:// { status: 'fulfilled', value: 100 }// { status: 'pending' }// { status: 'rejected', reason: Error: failed }

Note:ThepromiseState function still runs asynchronously, because there is no way to synchronously get a promise's value (i.e., withoutthen() orawait), even when it is already settled. However,promiseState() always fulfills within one tick and never actually waits for any promise's settlement.

Comparison with Promise.any()

Promise.race takes the first settledPromise.

js
const promise1 = new Promise((resolve, reject) => {  setTimeout(resolve, 500, "one");});const promise2 = new Promise((resolve, reject) => {  setTimeout(reject, 100, "two");});Promise.race([promise1, promise2])  .then((value) => {    console.log("succeeded with value:", value);  })  .catch((reason) => {    // Only promise1 is fulfilled, but promise2 is faster    console.error("failed with reason:", reason);  });// failed with reason: two

Promise.any takes the first fulfilledPromise.

js
const promise1 = new Promise((resolve, reject) => {  setTimeout(resolve, 500, "one");});const promise2 = new Promise((resolve, reject) => {  setTimeout(reject, 100, "two");});Promise.any([promise1, promise2])  .then((value) => {    // Only promise1 is fulfilled, even though promise2 settled sooner    console.log("succeeded with value:", value);  })  .catch((reason) => {    console.error("failed with reason:", reason);  });// succeeded with value: one

Specifications

Specification
ECMAScript® 2026 Language Specification
# sec-promise.race

Browser compatibility

See also

Help improve MDN

Learn how to contribute.

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp