QRG: await vs Promise.all: Awaiting Arrays of Promises
🧠 Quick Reference: Awaiting Arrays of Promises
❗ Problem
You called an async function that returns an array of promises and expected this to work:
const result = await myAsyncBatchFunction(); // ❌ Doesn't work as expected
But result is still an array of unresolved Promise objects.
✅ Fix
Use Promise.all(...) to await the entire array:
const result = await Promise.all(myAsyncBatchFunction()); // ✅ Now resolved
🔍 Why?
awaitunwraps one Promise- An array of promises is not a promise itself
Promise.all([...])returns a single promise that resolves to an array of resolved values
🧪 Example
async function fetchAll() {
return [fetchUser(), fetchPosts()];
}
const result = await fetchAll(); // ❌ result is [Promise, Promise]
const resolved = await Promise.all(fetchAll()); // ✅ resolved is [user, posts]
💡 Rule of Thumb
If you're awaiting something that returns multiple things, and they're async, wrap it in
Promise.all()— even if it's from anasyncfunction.