Full-stack Web Technologies

CHAPTER 5
Promise Static Methods

The Promise class has some static methods to help with promises.

Promise.resolve, Promise.reject

These are methods to produce an already resolved Promise and an already rejected Promise:

function adams() {
  return Promise.resolve(42);
}
function giveMeError(msg: string) {
  return Promise.reject(msg);
}

Promise.allSettled

Promise.allSettled is a function that receives an array of Promises and awaits all of them until all have either resolved or rejected (Promise.all is the old version, less convenient).

const apiGet = (url: string) => {
  const response = await fetch(url);
  return await response.json();
}

const [users, beers] = await Promise.allSettled([
  apiGet('https://randomuser.me/api/'),
  apiGet('https://api.punkapi.com/v2/beers'),
]);

The result is also an array (of the same size) with objects like:

[
  { status: "fulfilled", value: result },
  { status: "rejected", reason: error },
]