function delay(ms, val) {
return new Promise(r => setTimeout(() => r(val), ms));
}
async function run() {
const a = await delay(50, 'a');
const b = await delay(50, 'b');
console.log(a);
console.log(b);
}
run();function delay(ms, val) {
return new Promise(r => setTimeout(() => r(val), ms));
}
async function run() {
const [a, b] = await Promise.all([
delay(50, 'a'),
delay(50, 'b'),
]);
console.log(a);
console.log(b);
}
run();Bug: Sequential awaits mean b only starts after a resolves. Total time is ~100ms. These are independent and can run in parallel.
Explanation: Promise.all fires both at the same time. Total time is ~50ms. Output order follows the array order, not resolution order.
Key Insight: Sequential awaits for independent operations is a performance bug. Use Promise.all to run independent async operations in parallel.