Await On An Array Does Not Await Its Elements
await returns an array literal untouched, so its promise elements stay pending; Promise.all actually waits for every element and keeps the original order.
Code
let settled = 0;
const task = (name, ms) =>
new Promise((resolve) => setTimeout(() => { settled += 1; resolve(name); }, ms));
const first = task('first', 30);
const second = task('second', 10);
const arr = await [first, second];
console.log('elements still promises:', typeof arr[0].then === 'function', typeof arr[1].then === 'function');
console.log('tasks settled so far:', settled);
const [a, b] = await Promise.all([first, second]);
console.log('Promise.all results:', a, b);
console.log('tasks settled now:', settled);Output
Captured from a sandboxed run of this snippet in the build pipeline.
elements still promises: true true
tasks settled so far: 0
Promise.all results: first second
tasks settled now: 2The code at a glance — Await On An Array Does Not Await Its Elements
The simplest reading: the function task, doing all the work.
Asynchronous boundaries use await, so failures surface as rejected promises at the call site instead of being swallowed.
What to watch for
- Zero-fetch snippet: every identifier resolves inside the language distribution.
Using it
Worth reading first, the signature: const task = (name, ms)
The file’s steps show up in the output as elements still promises:, tasks settled so far:, Promise.all results:.
Key statements
new Promise((resolve) => setTimeout(() => { settled += 1; resolve(name); }, ms));
const [a, b] = await Promise.all([first, second]);
const arr = await [first, second];
Interactive Tools for This Topic
FAQ
- Which packages does this snippet need?
- None. Every symbol the code uses resolves inside the standard library. The file defines `task` at load time.
Report a problem with this exampleOpens a pre-filled issue on GitHub.