A. Promise.all requires an array of promises, but here arguments are passed separately.
B. Await cannot be used with Promise.all.
C. task1 and task2 must be awaited separately before Promise.all.
D. console.log cannot print arrays.
Solution
Step 1: Check Promise.all argument
Promise.all expects a single array of promises, but here two arguments are passed separately.
Step 2: Correct usage
It should be Promise.all([task1(), task2()]) with square brackets to group promises.
Final Answer:
Promise.all requires an array of promises, but here arguments are passed separately. -> Option A
Quick Check:
Promise.all needs array argument [OK]
Hint: Promise.all needs array, not separate args [OK]
Common Mistakes:
Passing promises as separate arguments
Thinking await can't be used with Promise.all
Misunderstanding console.log capabilities
5. You have three independent async tasks: taskA(), taskB(), and taskC(). You want to run taskA and taskB in parallel, then run taskC only after both finish. Which code correctly implements this?
hard
A. const c = await taskC();
const [a, b] = await Promise.all([taskA(), taskB()]);
B. await taskA(); await taskB(); await taskC();
C. const [a, b] = await Promise.all([taskA(), taskB()]);
const c = await taskC();
D. Promise.all([taskA(), taskB(), taskC()]);
Solution
Step 1: Run taskA and taskB in parallel
Using await Promise.all([taskA(), taskB()]) runs both tasks at the same time and waits for both to finish.
Step 2: Run taskC after both finish
After awaiting both, await taskC() runs taskC sequentially, ensuring it starts only after taskA and taskB complete.
Final Answer:
const [a, b] = await Promise.all([taskA(), taskB()]);
const c = await taskC(); -> Option C
Quick Check:
Parallel first, then sequential = A [OK]
Hint: Use Promise.all for parallel, then await next task [OK]