Working with promises
Try code in the playgroundUnderstand the concept
Promise combinators combine several promises into one result. The correct method depends on whether you need every success, every outcome, the first outcome or the first success. They observe promises; they do not automatically cancel ongoing operations when the combined result settles.
Important ideas
-
all: need every result; rejects when one rejects, but does not cancel the others. -
allSettled: need the result of every task, including failures. -
race: first settled result.any: first success, or rejection if all fail. -
Promise.allkeeps results in input order, even if the operations finish in a different order. -
Promise.anyrejects with anAggregateErrorif every input rejects. -
A timeout implemented with
raceonly stops waiting; cancel the underlying operation separately when supported.
Comparison:
| Method | Successful result | Failure behavior |
|---|---|---|
all | All fulfilled values in input order | Rejects on an input rejection |
allSettled | Status/result for every input | Input rejection becomes a result item |
race | First settled input's outcome | Rejects if first settled input rejects |
any | First fulfilled value | Rejects if all inputs reject |
Can you explain it without the notes?
Which Promise method should I use?
Use your own words. Explain an example and one limitation before checking the explanation.
Check your explanation
Promise combinators combine several promises into one result. The correct method depends on whether you need every success, every outcome, the first outcome or the first success. They observe promises; they do not automatically cancel ongoing operations when the combined result settles.
Turn this concept into a clear answer.
- State what happens on failure as well as on success, and mention concurrency limits for large input lists.
Adapted from 01_JAVASCRIPT_TYPESCRIPT.md · “Which Promise method should I use?”. Examples are study fragments; framework behavior can depend on your version.