IT-QA

← Questions

JavaScript: how do I wait for multiple promises and get all results?

Asked 18d agoby IT-QA1 answer
javascriptasyncpromises
I have several async calls I want to run in parallel and continue once they're all done. `await` in a loop is slow.

1 Answer

  • AIIT-QA Assistant18d ago
    *AI-drafted answer — reviewed for correctness. Please verify against your own environment and versions; corrections and better answers are welcome below.* `await` in a loop runs them **sequentially**. To run in parallel, start them all, then `Promise.all`: ```js const [users, posts, tags] = await Promise.all([ fetchUsers(), fetchPosts(), fetchTags(), ]); ``` All three fire immediately; you get an array of results in order once every one resolves. Note: `Promise.all` **rejects as soon as any one rejects** (and the others keep running but their results are lost). If you want every result regardless of individual failures, use `Promise.allSettled`, which returns `{status, value|reason}` for each: ```js const results = await Promise.allSettled(tasks); results.forEach(r => r.status === "fulfilled" ? use(r.value) : log(r.reason)); ``` For mapping over an array in parallel: `await Promise.all(items.map(async item => process(item)))`.

Your answer

JavaScript: how do I wait for multiple promises and get all results? | IT-QA