Javascript Promise.all( ) or Promise.settleAll( )
Promise.All()
Promise.all() is a JavaScript method that helps you run multiple asynchronous tasks at the same time. Instead of waiting for each task to finish one by one, it runs them together and gives you the result once all tasks are completed successfully. If even one task fails, Promise.all() stops and returns an error.
It is used to execute multiple Promises concurrently and aggregate their results into a single Promise. It resolves with an array of resolved values when all Promises succeed, preserving their original order, and rejects immediately if any Promise fails, following a fail-fast execution model.
Think of Promise.all() as: "Run everything together. If one fails, abort the result." It is commonly used to fetch data from multiple APIs in parallel rather than sequentially, which significantly improves performance by reducing total execution time.
Promise.allSettled()
Promise.allSettled() is used to execute multiple asynchronous operations in parallel and wait for all of them to finish, regardless of whether they succeed or fail. Unlike Promise.all(), it never rejects and instead returns the outcome of each Promise.
It runs all async tasks in parallel and gives a complete success/failure report without stopping on errors. “Run everything. Tell me what succeeded and what failed.”
Behavior Comparison Table
| Feature | Promise.all() | Promise.allSettled() |
|---|---|---|
| Waits for all promises | ❌ No | ✅ Yes |
| Stops on first failure | ✅ Yes | ❌ No |
| Rejects | ✅ Yes | ❌ Never |
| Returns | Array of values | Array of result objects |
| Use case | All-or-nothing | Best-effort / partial success |
Comments
Post a Comment