Sobes.tech
Middle+

What static methods does the Promise class have?

sobes.tech AI

Answer from AI

The Promise class in JavaScript has several static methods that allow working with multiple promises or creating already resolved/rejected promises:

  • Promise.resolve(value) — returns a promise that is immediately resolved with the value value.
  • Promise.reject(reason) — returns a promise that is immediately rejected with the reason reason.
  • Promise.all(iterable) — takes an iterable of promises and returns a promise that resolves with an array of results if all promises succeed, or rejects as soon as one promise rejects.
  • Promise.race(iterable) — returns a promise that resolves or rejects as soon as one of the promises in the iterable resolves or rejects.
  • Promise.allSettled(iterable) — returns a promise that resolves after all promises have settled, with an array of their results (success or failure).
  • Promise.any(iterable) — returns a promise that resolves with the result of the first successfully fulfilled promise, or rejects if all promises are rejected.

Example usage:

Promise.all([promise1, promise2])
  .then(results => console.log(results))
  .catch(error => console.error(error));