0
0
Node.jsframework~20 mins

Error-first callback convention in Node.js - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Error-first Callback Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Understanding error-first callback output
What will be the output of this Node.js code using an error-first callback?
Node.js
function fetchData(callback) {
  setTimeout(() => {
    callback(null, 'Data loaded');
  }, 100);
}

fetchData((err, data) => {
  if (err) {
    console.log('Error:', err);
  } else {
    console.log('Success:', data);
  }
});
AError: null
BSuccess: null
CSuccess: Data loaded
DError: Data loaded
Attempts:
2 left
💡 Hint
Remember, the first argument is the error, the second is the data.
Predict Output
intermediate
2:00remaining
Detecting error in error-first callback
What will this code print when the callback receives an error?
Node.js
function readFile(callback) {
  setTimeout(() => {
    callback(new Error('File not found'), null);
  }, 50);
}

readFile((err, content) => {
  if (err) {
    console.log('Error:', err.message);
  } else {
    console.log('Content:', content);
  }
});
AContent: File not found
BContent: null
CError: null
DError: File not found
Attempts:
2 left
💡 Hint
Check how the error object is passed and accessed.
component_behavior
advanced
2:30remaining
Behavior of nested error-first callbacks
Consider this nested callback code. What will be logged?
Node.js
function step1(cb) {
  setTimeout(() => cb(null, 'Step1 done'), 30);
}
function step2(cb) {
  setTimeout(() => cb(new Error('Step2 failed'), null), 20);
}

step1((err, res1) => {
  if (err) {
    console.log('Error in step1:', err.message);
  } else {
    step2((err, res2) => {
      if (err) {
        console.log('Error in step2:', err.message);
      } else {
        console.log('Success:', res2);
      }
    });
  }
});
AError in step1: Step2 failed
BError in step2: Step2 failed
CSuccess: Step2 failed
DSuccess: Step1 done
Attempts:
2 left
💡 Hint
Look carefully at which callback receives the error.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in error-first callback usage
Which option contains a syntax error in the error-first callback pattern?
Node.js
function getData(callback) {
  setTimeout(() => {
    callback(null, 'OK');
  }, 10);
}

getData((err data) => {
  if (err) {
    console.log('Error:', err);
  } else {
    console.log('Data:', data);
  }
});
AgetData((err data) => { console.log(data); });
BgetData((err, data) => { console.log(data); });
CgetData(function(err, data) { console.log(data); });
DgetData((error, result) => { console.log(result); });
Attempts:
2 left
💡 Hint
Check the arrow function parameter list syntax.
🔧 Debug
expert
3:00remaining
Debugging incorrect error handling in callback
What is the main problem with this error-first callback usage?
Node.js
function processData(callback) {
  setTimeout(() => {
    callback(null, 'Done');
  }, 10);
}

processData((err, data) => {
  if (!err) {
    console.log('Error:', err.message);
  } else {
    console.log('Success:', data);
  }
});
AIt logs 'Error: Cannot read property message of null' because err is null but accessed as an object.
BIt logs 'Success: Done' correctly.
CIt throws a syntax error due to wrong if condition.
DIt never logs anything because callback is not called.
Attempts:
2 left
💡 Hint
Check how err is tested and used inside the callback.