Challenge - 5 Problems
Master of Sequential and Parallel Execution
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of sequential async calls
What will be the output of this JavaScript code that runs two async functions sequentially?
Javascript
async function task(name, delay) { return new Promise(resolve => { setTimeout(() => { console.log(name); resolve(name); }, delay); }); } async function run() { await task('First', 100); await task('Second', 50); console.log('Done'); } run();
Attempts:
2 left
💡 Hint
Remember that await pauses the function until the promise resolves.
✗ Incorrect
The first task waits 100ms and prints 'First'. Only after it finishes, the second task starts and prints 'Second' after 50ms. Finally, 'Done' is printed.
❓ Predict Output
intermediate2:00remaining
Output of parallel async calls with Promise.all
What will be the output of this JavaScript code that runs two async functions in parallel?
Javascript
async function task(name, delay) { return new Promise(resolve => { setTimeout(() => { console.log(name); resolve(name); }, delay); }); } async function run() { await Promise.all([task('First', 100), task('Second', 50)]); console.log('Done'); } run();
Attempts:
2 left
💡 Hint
Promise.all runs promises in parallel and waits for all to finish.
✗ Incorrect
Both tasks start at the same time. 'Second' finishes first after 50ms, then 'First' after 100ms. After both finish, 'Done' is printed.
❓ Predict Output
advanced2:00remaining
Output of mixed sequential and parallel async calls
What will be the output of this code that mixes sequential and parallel async calls?
Javascript
async function task(name, delay) { return new Promise(resolve => { setTimeout(() => { console.log(name); resolve(name); }, delay); }); } async function run() { await task('First', 100); await Promise.all([task('Second', 50), task('Third', 70)]); console.log('Done'); } run();
Attempts:
2 left
💡 Hint
The first task runs alone, then the next two run together.
✗ Incorrect
The first task prints 'First' after 100ms. Then 'Second' and 'Third' run in parallel; 'Second' prints after 50ms, 'Third' after 70ms. Finally, 'Done' is printed.
❓ Predict Output
advanced2:00remaining
Output order with Promise.race
What will be the output of this code using Promise.race with two async tasks?
Javascript
async function task(name, delay) { return new Promise(resolve => { setTimeout(() => { console.log(name); resolve(name); }, delay); }); } async function run() { await Promise.race([task('First', 100), task('Second', 50)]); console.log('Done'); } run();
Attempts:
2 left
💡 Hint
Promise.race resolves as soon as the first promise resolves.
✗ Incorrect
'Second' finishes first after 50ms and prints 'Second'. Promise.race resolves then, so 'Done' is printed next. 'First' prints later but after 'Done'.
🧠 Conceptual
expert2:00remaining
Understanding event loop and parallelism in JavaScript
Which statement best describes how JavaScript handles parallel execution and the event loop?
Attempts:
2 left
💡 Hint
Think about how JavaScript handles async tasks without real threads.
✗ Incorrect
JavaScript runs on a single thread but uses the event loop to handle asynchronous tasks, allowing non-blocking behavior and simulating parallelism.