0
0
Node.jsframework~20 mins

Callback pattern and callback hell in Node.js - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Callback Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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();
  });
});
A"first", "third", "second"
B"first", "second", "third"
C"second", "first", "third"
D"third", "first", "second"
Attempts:
2 left
💡 Hint
Remember that setTimeout with 0 delay runs after the current call stack.
📝 Syntax
intermediate
2: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);
  }
});
AfetchData((err, data) => { if (err) { console.error(err); } else { console.log(data); } });
B;)} } ;)atad(gol.elosnoc { esle } ;)rre(rorre.elosnoc { )rre( fi { >= )atad ,rre((ataDhctef
CfetchData(function(err, data) { if (err) { console.error(err); } else { console.log(data); } });
D;)} } ;)atad(gol.elosnoc { esle } ;)rre(rorre.elosnoc { )rre( fi { )atad ,rre(noitcnuf(ataDhctef
Attempts:
2 left
💡 Hint
Look carefully at the if-else statement syntax.
🔧 Debug
advanced
2: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');
    });
  });
});
AThe setTimeout delays are too long causing performance issues.
BThe callbacks are not called, so the code never runs.
CThe code causes callback hell making it hard to read and maintain.
DThe code will throw a runtime error due to missing callback arguments.
Attempts:
2 left
💡 Hint
Look at how callbacks are nested inside each other.
state_output
advanced
2: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);
      }
    });
  }
});
A"result1" followed by "Error: error in task2"
B"result1" followed by "undefined"
C"Error: error in task2" followed by "result1"
DNo output due to uncaught error
Attempts:
2 left
💡 Hint
Check how errors are handled in each callback.
🧠 Conceptual
expert
2: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?
ACallback hell happens because callbacks run too fast; adding delays fixes it.
BCallback hell happens because callbacks are not typed; using TypeScript solves it.
CCallback hell happens because callbacks are synchronous; making them asynchronous avoids it.
DCallback hell happens because callbacks are nested deeply; using Promises or async/await helps flatten the code.
Attempts:
2 left
💡 Hint
Think about code readability and modern JavaScript features.