JS: Promise.allSettled

By Xah Lee. Date: .

(new in JS: ECMAScript 2020)

Promise.allSettled(iterable)

iterable is a Iterable Object. Each yield should be a promise. If not, it is treated as resolved value of fulfilled promise.

returns a promise. the promise state is fulfilled when all input promises has settled (either fulfilled or rejected).

the result promise is never in rejected state.

The fulfilled value is an array of the input promise's resolution values, in the same order.

const p1 = Promise.resolve(42);
const p2 = Promise.reject("fail");
const p3 = new Promise((resolve) => setTimeout(() => resolve("late"), 100));

Promise.allSettled([p1, p2, p3])
 .then(
  (x) => console.log("fulfilled with value:", x),
  (x) => console.log("rejected with value:", x),
 );

/*
fulfilled with value:
[
  { status: "fulfilled", value: 42 },
  { status: "rejected", reason: "fail" },
  { status: "fulfilled", value: "late" }
]
*/