Challenge - 5 Problems
Callback Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
Understanding callback execution order
Consider the following Node.js code using callbacks. What will be the order of console output?
Node.js
function first(callback) {
console.log('first');
callback();
}
function second(callback) {
setTimeout(() => {
console.log('second');
callback();
}, 0);
}
function third() {
console.log('third');
}
first(() => {
second(() => {
third();
});
});Attempts:
2 left
💡 Hint
Remember that setTimeout with 0 delay runs after the current call stack.
✗ Incorrect
The first function logs immediately, then calls second. The second function uses setTimeout with 0 delay, so its callback runs after the current stack finishes. Finally, third runs after second completes.
📝 Syntax
intermediate2:00remaining
Identify the syntax error in callback usage
Which option contains a syntax error that will prevent the code from running?
Node.js
function fetchData(callback) {
setTimeout(() => {
callback(null, 'data');
}, 100);
}
fetchData((err, data) => {
if (err) {
console.error(err);
} else {
console.log(data);
}
});Attempts:
2 left
💡 Hint
Look carefully at the if-else statement syntax.
✗ Incorrect
Option A is missing a closing brace before the else, causing a syntax error.
🔧 Debug
advanced2:00remaining
Debugging callback hell with nested callbacks
What is the main problem with the following code snippet?
Node.js
function step1(callback) {
setTimeout(() => {
console.log('step1 done');
callback();
}, 100);
}
function step2(callback) {
setTimeout(() => {
console.log('step2 done');
callback();
}, 100);
}
function step3(callback) {
setTimeout(() => {
console.log('step3 done');
callback();
}, 100);
}
step1(() => {
step2(() => {
step3(() => {
console.log('All steps done');
});
});
});Attempts:
2 left
💡 Hint
Look at how callbacks are nested inside each other.
✗ Incorrect
The code works but nesting callbacks deeply creates 'callback hell', which is hard to read and maintain.
❓ state_output
advanced2:00remaining
What is the output of nested callbacks with error handling?
Given the code below, what will be printed to the console?
Node.js
function task1(callback) {
setTimeout(() => {
callback(null, 'result1');
}, 50);
}
function task2(callback) {
setTimeout(() => {
callback('error in task2');
}, 50);
}
task1((err, res) => {
if (err) {
console.log('Error:', err);
} else {
console.log(res);
task2((err2, res2) => {
if (err2) {
console.log('Error:', err2);
} else {
console.log(res2);
}
});
}
});Attempts:
2 left
💡 Hint
Check how errors are handled in each callback.
✗ Incorrect
First, task1 returns result1 with no error, so it prints result1. Then task2 returns an error, so it prints Error: error in task2.
🧠 Conceptual
expert2:00remaining
Why does callback hell occur and how to avoid it?
Which option best explains why callback hell happens and a common way to avoid it in Node.js?
Attempts:
2 left
💡 Hint
Think about code readability and modern JavaScript features.
✗ Incorrect
Callback hell occurs due to deeply nested callbacks making code hard to read. Using Promises or async/await flattens the structure and improves readability.