Middle
Tell me about Promise: states, static methods (all, any, allSettled, race). What does Promise.all return if one of the promises fails?
sobes.tech AI
Answer from AI
Promise is an object representing the result of an asynchronous operation. It has three states:
- pending
- fulfilled
- rejected
Static methods:
Promise.all(iterable)— waits for all promises to settle. If all succeed, returns an array of results. If at least one is rejected, returns the error of the first rejected promise.Promise.any(iterable)— waits for the first successfully fulfilled promise. If all are rejected, returns an AggregateError.Promise.allSettled(iterable)— waits for all promises to settle, returning an array of objects with results and statuses, regardless of success or failure.Promise.race(iterable)— returns the result of the first promise to settle, whether fulfilled or rejected.
If one of the promises in Promise.all is rejected, the entire Promise.all immediately rejects with that error, and further results are ignored.