Promise.methods

Notes about Promise methods


article image
Pinky promise!!

Promise.all

“The Promise.all() method takes an iterable of promises as an input, and returns a single Promise that resolves to an array of the results of the input promises.

This returned promise will fulfill when all of the input’s promises have fulfilled, or if the input iterable contains no promises.

It rejects immediately upon any of the input promises rejecting or non-promises throwing an error, and will reject with this first rejection message/error.”

Promise.resolve

“The Promise.resolve() method “resolves” a given value to a Promise. If the value is a promise, that promise is returned; if the value is a thenable, Promise.resolve() will call the then() method with two callbacks it prepared; otherwise, the returned promise will be fulfilled with the value.”

Promise.reject

“The Promise.reject() method returns a Promise object that is rejected with a given reason.”

Promise.race

“The Promise.race() method returns a promise that fulfills or rejects as soon as one of the promises in an iterable fulfills or rejects, with the value or reason from that promise.”


Promise.any

"Fulfills when any of the input's promises fulfills, with this first fulfillment value. It rejects when all of the input's promises reject (including when an empty iterable is passed)"

Promise.allSettled

“The Promise.allSettled() method returns a promise that fulfills after all of the given promises have either fulfilled or rejected, with an array of objects that each describes the outcome of each promise.

It is typically used when you have multiple asynchronous tasks that are not dependent on one another to complete successfully, or you’d always like to know the result of each promise.

In comparison, the Promise returned by Promise.all() may be more appropriate if the tasks are dependent on each other/if you’d like to immediately reject upon any of them rejecting.”


Don't be shy, leave us a comment