Hey friends,
Promise.all runs several promises at once and fulfills with an array of every result, in input order, but rejects as soon as any one of them rejects. That's from MDN's Promise.all page. Its three siblings, allSettled, any and race, cover the cases where that all-or-nothing behavior isn't what you want.
Picking the right one is a classic scenario question in the Asynchronous Programming section of the Salesforce JavaScript Developer exam. Here's the comparison, then examples.
What's the difference between all, allSettled, any and race?
| Method | Fulfills when | Rejects when | Use it for |
|---|---|---|---|
| Promise.all | All inputs fulfill. Value: array of results in input order. | Any input rejects (with that first reason) | Loading things you need all of |
| Promise.allSettled | All inputs settle, either way. Value: array of status objects. | Never, for an iterable of promises | Batch work where you report each outcome |
| Promise.any | The first input fulfills | All inputs reject, with an AggregateError | Fastest successful source, like mirrors |
| Promise.race | The first input to settle fulfills | The first input to settle rejects | Timeouts |
An empty array is the edge case: Promise.all and allSettled fulfill right away with [], and Promise.any rejects with an AggregateError.
How do you use Promise.all?
The three calls start together, so the total time is about the slowest one, not the sum. Results come back in the order you passed them, no matter which finishes first. Non-promise values in the array are passed through as-is.
Important: when one rejects, Promise.all rejects, but the other operations keep running. JavaScript has no built-in way to cancel them. Their results are just ignored.
When should you use Promise.allSettled?
When partial success is fine and you want to know what happened to each one. Each result is an object with status of "fulfilled" and a value, or "rejected" and a reason.
Cert Lab by Nick Frates
Let the JavaScript Developer Cert Lab test you to success
Pick-the-right-API scenarios on promises, async/await and the rest of the exam, with an explanation for every option.
Start the JavaScript Developer Cert Lab for $9.99 →Not ready to buy? Try a free lesson and see how fast you learn.
When should you use Promise.any?
When you need one success and don't care which. It ignores rejections until all of them have failed. Then it rejects with an AggregateError whose errors array holds every reason, in input order.
When should you use Promise.race?
When the first one to finish wins, success or failure. The classic use is a timeout:
any vs race, the one the exam likes: if the first promise to settle rejects, race rejects, but any keeps waiting for a fulfillment.
What Promise.all mistakes should you avoid?
- Passing functions instead of promises.
Promise.all([getA, getB])resolves to the functions themselves. Call them:[getA(), getB()]. - Using all when one failure shouldn't sink the batch. Use allSettled.
- Firing thousands of requests at once. Promise.all doesn't limit concurrency. Process in chunks.
- Assuming a rejection cancels the rest. It doesn't.
Back to basics: JavaScript promises explained and async/await.
Quick backstory. I loved Focus on Force and used it on my way to 17 Salesforce certifications. At first I read the study guide cover to cover, then took the tests. What I eventually noticed: I learned way faster when I skipped ahead and took practice tests rapid-fire, reading the explanation for every miss. So I built Cert Lab around fast testing, with up-to-date doc links in every explanation. It's designed to test you to success.
FAQ
Does Promise.all run in parallel?
The operations run concurrently: they all start before any finishes. Your JavaScript callbacks still run one at a time on a single thread.
What order does Promise.all return results in?
Input order, regardless of which promise finishes first.
What happens if one promise in Promise.all rejects?
The returned promise rejects with that reason right away. The other operations keep going, but their results are ignored.
Which Promise method never rejects?
Promise.allSettled, when you give it an iterable of promises. See the JavaScript Developer study guide for the full async section.
Where should you go next?
- The JavaScript event loop for when then callbacks run.
- JavaScript Developer practice questions to test yourself.
- JavaScript Developer study guide for every exam section.
Nick Frates, a senior Salesforce developer with 17 certifications, built the JavaScript Developer Cert Lab at nickfrates.com: rapid-fire practice questions for every exam section, each answer explained.
Cert Lab by Nick Frates
Let the JavaScript Developer Cert Lab test you to success
Lessons, flashcards, a quiz for each of the seven exam sections and full practice exams, every answer explained.
Start the JavaScript Developer Cert Lab for $9.99 →Not ready to buy? Try a free lesson and see how fast you learn.
Hope this helps!
Best,
Nick


