Performance: Promise.all for parallel execution
This affects how quickly multiple asynchronous tasks complete and how the event loop handles concurrency.
Jump into concepts and practice - no test required
async function fetchParallel() { const results = await Promise.all([fetch(url1), fetch(url2)]); return results; }
async function fetchSequential() { const result1 = await fetch(url1); const result2 = await fetch(url2); return [result1, result2]; }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Sequential await calls | N/A | N/A | Blocks event loop longer | [X] Bad |
| Promise.all parallel execution | N/A | N/A | Non-blocking, faster completion | [OK] Good |
Promise.all do in Node.js?Promise.all with an array of promises named promises?const p1 = Promise.resolve(1); const p2 = Promise.resolve(2); const p3 = Promise.resolve(3); Promise.all([p1, p2, p3]).then(results => console.log(results));
const p1 = Promise.resolve('A');
const p2 = Promise.reject('Error');
Promise.all([p1, p2])
.then(results => console.log('Results:', results))
.catch(error => console.log('Caught:', error));Promise.all to achieve this?const fetch1 = () => fetch('https://api1.example.com/data').then(res => res.json());
const fetch2 = () => fetch('https://api2.example.com/data').then(res => res.json());
const fetch3 = () => fetch('https://api3.example.com/data').then(res => res.json());
// Which code snippet correctly waits for all fetches and handles errors?