What are Error-First Callbacks in Node.js?
Learn the Node.js error-first callback convention: the (err, result) signature, why you check err first, and how util.promisify turns it into a promise.
Expected Interview Answer
An error-first callback is a Node.js convention where a callback function takes the error as its first argument and the successful result as later arguments, so you always check err before using the data.
This pattern, sometimes called the Node-style callback, standardises asynchronous APIs across the ecosystem. The callback signature is (err, result): if the operation fails, err holds an Error object and result is usually undefined; if it succeeds, err is null and the result arguments carry the data. Because every core API follows it, developers can handle errors uniformly and tools like util.promisify can automatically convert these callbacks into promises.
- One consistent error-handling convention across all Node APIs
- Forces callers to consider failure before using results
- Easy to wrap with util.promisify for async/await
- Predictable callback signature for library authors
- Keeps error data flowing through asynchronous boundaries
AI Mentor Explanation
An error-first callback is like a third umpire's decision that always states 'out or not out' before anything else. The captain checks that verdict first: if it's 'not out' due to a no-ball, play is corrected before celebrating; only once the first word confirms all is fine do you act on the runs scored, never assuming the outcome before hearing the ruling.
Step-by-Step Explanation
Step 1
Define the signature
Write callbacks as (err, result) so the error slot always comes first.
Step 2
Check err first
Inside the callback, test if (err) before touching result and handle or propagate the failure.
Step 3
Return early on error
Use return callback(err) or throw to stop, avoiding use of undefined result data.
Step 4
Pass null on success
When the operation succeeds, call the callback with null as the error and the data afterwards.
Step 5
Promisify when needed
Wrap the callback API with util.promisify to consume it with async/await.
What Interviewer Expects
- Knowing the (err, result) callback signature
- Always checking err before using the result
- Understanding it is a convention, not a language feature
- Awareness that core Node APIs follow this pattern
- Ability to convert to promises with util.promisify
Common Mistakes
- Using the result before checking the error argument
- Forgetting to return after handling an error, causing double execution
- Passing the error as the second argument instead of the first
- Not passing null as the error on success
- Swallowing errors silently instead of propagating them
Best Answer (HR Friendly)
“An error-first callback is a Node.js habit where the function that runs after an async task always receives any error as its first piece of information. You check that error first, and only if there is none do you use the actual result, which keeps error handling consistent everywhere.”
Code Example
const fs = require('fs');
fs.readFile('config.json', 'utf8', (err, data) => {
if (err) {
// err is the FIRST argument — handle it before anything else
console.error('Failed to read file:', err.message);
return; // stop so we never use an undefined `data`
}
// Only reached when err is null
const config = JSON.parse(data);
console.log('Loaded config:', config);
});function divide(a, b, callback) {
if (b === 0) {
return callback(new Error('Cannot divide by zero'));
}
// success: null error, then the result
callback(null, a / b);
}
divide(10, 2, (err, result) => {
if (err) return console.error(err.message);
console.log('Result:', result); // Result: 5
});Follow-up Questions
- How does util.promisify turn an error-first callback into a promise?
- Why is checking err before result important?
- What problems does callback hell cause and how do promises help?
- How do you propagate an error through nested callbacks?
- What happens if you forget to return after calling back with an error?
MCQ Practice
1. In an error-first callback, what is the first argument?
By convention the first argument is the error (null on success), followed by the result data.
2. On a successful operation, what should the error argument be?
Success is signalled by passing null (or nothing) as the error and providing the result afterwards.
3. Which utility converts an error-first callback function into a promise?
util.promisify wraps a standard error-first callback function so it returns a promise usable with async/await.
Flash Cards
What is the error-first callback signature? — (err, result) — the error comes first, the successful data comes after.
What value is err on success? — null (or undefined), with the actual result passed as the following arguments.
What must you do before using result? — Check if (err) and handle or return the error first.
How do you convert it to a promise? — Wrap the function with util.promisify to use async/await.