Movatterモバイル変換


[0]ホーム

URL:


MDN Web Docs

Promise.all()

BaselineWidely available

ThePromise.all() static method takes an iterable of promises as input and returns a singlePromise. This returned promise fulfills when all of the input's promises fulfill (including when an empty iterable is passed), with an array of the fulfillment values. It rejects when any of the input's promises rejects, with this first rejection reason.

Try it

const promise1 = Promise.resolve(3);const promise2 = 42;const promise3 = new Promise((resolve, reject) => {  setTimeout(resolve, 100, "foo");});Promise.all([promise1, promise2, promise3]).then((values) => {  console.log(values);});// Expected output: Array [3, 42, "foo"]

Syntax

js
Promise.all(iterable)

Parameters

iterable

Aniterable (such as anArray) of promises.

Return value

APromise that is:

  • Already fulfilled, if theiterable passed is empty.
  • Asynchronously fulfilled, when all the promises in the giveniterable fulfill. The fulfillment value is an array of fulfillment values, in the order of the promises passed, regardless of completion order. If theiterable passed is non-empty but contains no pending promises, the returned promise is still asynchronously (instead of synchronously) fulfilled.
  • Asynchronously rejected, when any of the promises in the giveniterable rejects. The rejection reason is the rejection reason of the first promise that was rejected.

Description

ThePromise.all() method is one of thepromise concurrency methods. It can be useful for aggregating the results of multiple promises. It is typically used when there are multiple related asynchronous tasks that the overall code relies on to work successfully — all of whom we want to fulfill before the code execution continues.

Promise.all() will reject immediately uponany of the input promises rejecting. In comparison, the promise returned byPromise.allSettled() will wait for all input promises to complete, regardless of whether or not one rejects. UseallSettled() if you need the final result of every promise in the input iterable.

Examples

Using Promise.all()

Promise.all waits for all fulfillments (or the first rejection).

js
const p1 = Promise.resolve(3);const p2 = 1337;const p3 = new Promise((resolve, reject) => {  setTimeout(() => {    resolve("foo");  }, 100);});Promise.all([p1, p2, p3]).then((values) => {  console.log(values); // [3, 1337, "foo"]});

If theiterable contains non-promise values, they will be ignored, but still counted in the returned promise array value (if the promise is fulfilled):

js
// All values are non-promises, so the returned promise gets fulfilledconst p = Promise.all([1, 2, 3]);// The only input promise is already fulfilled,// so the returned promise gets fulfilledconst p2 = Promise.all([1, 2, 3, Promise.resolve(444)]);// One (and the only) input promise is rejected,// so the returned promise gets rejectedconst p3 = Promise.all([1, 2, 3, Promise.reject(new Error("bad"))]);// Using setTimeout, we can execute code after the queue is emptysetTimeout(() => {  console.log(p);  console.log(p2);  console.log(p3);});// Logs:// Promise { <state>: "fulfilled", <value>: Array[3] }// Promise { <state>: "fulfilled", <value>: Array[4] }// Promise { <state>: "rejected", <reason>: Error: bad }

Asynchronicity or synchronicity of Promise.all

This following example demonstrates the asynchronicity ofPromise.all when a non-emptyiterable is passed:

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

The same thing happens ifPromise.all rejects:

js
const mixedPromisesArray = [  Promise.resolve(33),  Promise.reject(new Error("bad")),];const p = Promise.all(mixedPromisesArray);console.log(p);setTimeout(() => {  console.log("the queue is now empty");  console.log(p);});// Logs:// Promise { <state>: "pending" }// the queue is now empty// Promise { <state>: "rejected", <reason>: Error: bad }

Promise.all resolves synchronously if and only if theiterable passed is empty:

js
const p = Promise.all([]); // Will be immediately resolvedconst p2 = Promise.all([1337, "hi"]); // Non-promise values are ignored, but the evaluation is done asynchronouslyconsole.log(p);console.log(p2);setTimeout(() => {  console.log("the queue is now empty");  console.log(p2);});// Logs:// Promise { <state>: "fulfilled", <value>: Array[0] }// Promise { <state>: "pending" }// the queue is now empty// Promise { <state>: "fulfilled", <value>: Array[2] }

Using Promise.all() with async functions

Withinasync functions, it's very common to "over-await" your code. For example, given the following functions:

js
function promptForDishChoice() {  return new Promise((resolve, reject) => {    const dialog = document.createElement("dialog");    dialog.innerHTML = `<form method="dialog">  <p>What would you like to eat?</p>  <select>    <option value="pizza">Pizza</option>    <option value="pasta">Pasta</option>    <option value="salad">Salad</option>  </select>  <menu>    <li><button value="cancel">Cancel</button></li>    <li><button type="submit" value="ok">OK</button></li>  </menu></form>    `;    dialog.addEventListener("close", () => {      if (dialog.returnValue === "ok") {        resolve(dialog.querySelector("select").value);      } else {        reject(new Error("User cancelled dialog"));      }    });    document.body.appendChild(dialog);    dialog.showModal();  });}async function fetchPrices() {  const response = await fetch("/prices");  return await response.json();}

You may write a function like this:

js
async function getPrice() {  const choice = await promptForDishChoice();  const prices = await fetchPrices();  return prices[choice];}

However, note that the execution ofpromptForDishChoice andfetchPrices don't depend on the result of each other. While the user is choosing their dish, it's fine for the prices to be fetched in the background, but in the code above, theawait operator causes the async function to pause until the choice is made, and then again until the prices are fetched. We can usePromise.all to run them concurrently, so that the user doesn't have to wait for the prices to be fetched before the result is given:

js
async function getPrice() {  const [choice, prices] = await Promise.all([    promptForDishChoice(),    fetchPrices(),  ]);  return prices[choice];}

Promise.all is the best choice ofconcurrency method here, because error handling is intuitive — if any of the promises reject, the result is no longer available, so the wholeawait expression throws.

Promise.all accepts an iterable of promises, so if you are using it to run several async functions concurrently, you need to call the async functions and use the returned promises. Directly passing the functions toPromise.all does not work, since they are not promises.

js
async function getPrice() {  const [choice, prices] = await Promise.all([    promptForDishChoice,    fetchPrices,  ]);  // `choice` and `prices` are still the original async functions;  // Promise.all() does nothing to non-promises}

Promise.all fail-fast behavior

Promise.all is rejected if any of the elements are rejected. For example, if you pass in four promises that resolve after a timeout and one promise that rejects immediately, thenPromise.all will reject immediately.

js
const p1 = new Promise((resolve, reject) => {  setTimeout(() => resolve("one"), 1000);});const p2 = new Promise((resolve, reject) => {  setTimeout(() => resolve("two"), 2000);});const p3 = new Promise((resolve, reject) => {  setTimeout(() => resolve("three"), 3000);});const p4 = new Promise((resolve, reject) => {  setTimeout(() => resolve("four"), 4000);});const p5 = new Promise((resolve, reject) => {  reject(new Error("reject"));});// Using .catch:Promise.all([p1, p2, p3, p4, p5])  .then((values) => {    console.log(values);  })  .catch((error) => {    console.error(error.message);  });// Logs:// "reject"

It is possible to change this behavior by handling possible rejections:

js
const p1 = new Promise((resolve, reject) => {  setTimeout(() => resolve("p1_delayed_resolution"), 1000);});const p2 = new Promise((resolve, reject) => {  reject(new Error("p2_immediate_rejection"));});Promise.all([p1.catch((error) => error), p2.catch((error) => error)]).then(  (values) => {    console.log(values[0]); // "p1_delayed_resolution"    console.error(values[1]); // "Error: p2_immediate_rejection"  },);

Specifications

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

Browser compatibility

See also

Help improve MDN

Learn how to contribute.

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp