async function first() { console.log('First start'); await new Promise(resolve => setTimeout(resolve, 100)); console.log('First end'); } async function second() { console.log('Second start'); await new Promise(resolve => setTimeout(resolve, 50)); console.log('Second end'); } first(); second();
Calling async functions without awaiting starts them immediately. Both 'First start' and 'Second start' log quickly. Then the shorter timeout in second() resolves first, so 'Second end' logs before 'First end'.
async function asyncTask() { return new Promise(resolve => setTimeout(resolve, 100)); } // Snippet 1 async function sequential() { await asyncTask(); await asyncTask(); } // Snippet 2 async function parallel() { await Promise.all([asyncTask(), asyncTask()]); }
Sequential awaits each task one after the other, so total time adds up. Parallel runs both tasks simultaneously, so total time is about the longest single task.
async function fetchData(id) { return new Promise(resolve => setTimeout(() => resolve(id), 100)); } async function run() { const results = await Promise.all(fetchData(1), fetchData(2)); console.log(results); } run();
Promise.all expects a single iterable (like an array) of promises. Passing multiple arguments causes a TypeError.
let count = 0; function delayAdd(value) { return new Promise(resolve => setTimeout(() => { count += value; resolve(count); }, 100)); } async function sequential() { await delayAdd(1); await delayAdd(2); } async function parallel() { await Promise.all([delayAdd(1), delayAdd(2)]); }
Both sequential and parallel add 1 and 2 to count. Sequentially, count updates stepwise. Parallel updates happen concurrently but both increments apply, so total is 3 in both cases.
async function task1() { throw new Error('fail1'); } async function task2() { return 'success2'; } async function run() { // Fill in the blank }
Option A correctly uses Promise.all with an array and wraps it in try/catch to handle errors. Option A uses allSettled which doesn't throw but returns statuses. Option A passes multiple arguments to Promise.all causing error. Option A logs results after catch but results is undefined if error occurs.