Error-First Callback Pattern (Node.js)
MedThe error-first callback pattern is a convention in Node.js where callbacks always receive an error object as the first argument. If an error occurred, the first argument contains the error; otherwise, it is null. The second argument contains the successful result. This pattern became the standard in Node.js core APIs.
Interactive Visualization
Path Taken
Error Path
(not taken)
Success Path
(not taken)
Callback Chain
readFile callbackpending
CodeCalling
1const fs = require('fs');23fs.readFile('data.txt', 'utf8', (err, data) => {4 if (err) {5 console.log("Error:", err.message);6 return;7 }8 console.log("Data:", data);9});
Output
-
We call fs.readFile with a callback that follows the (err, data) signature.
1 / 4
Key Insight: The error-first pattern: always check err FIRST. If null/undefined, proceed with data.
Key Points
- First parameter is always an error object (or null if no error)
- Second parameter contains the successful result
- Standard pattern in Node.js core modules (fs, http, etc.)
- Always check if error exists before using result
- Promises and async/await have largely replaced this pattern
Code Examples
Basic Error-First Pattern
// Node.js fs module example const fs = require('fs'); fs.readFile('file.txt', 'utf8', function(error, data) { // First parameter: error (null if success) // Second parameter: result if (error) { console.error('Error reading file:', error.message); return; } console.log('File contents:', data); });
The callback receives (error, result). Always check error first!
Writing Your Own Error-First Function
function divide(a, b, callback) { if (b === 0) { // Pass error as first argument callback(new Error('Cannot divide by zero'), null); return; } // Pass null as error, result as second argument callback(null, a / b); } // Usage divide(10, 2, function(error, result) { if (error) { console.error(error.message); return; } console.log('Result:', result); // 5 }); divide(10, 0, function(error, result) { if (error) { console.error(error.message); // "Cannot divide by zero" } });
When implementing, pass Error object first on failure, null first on success
Common Mistake: Ignoring Error
// ❌ WRONG: Not checking error first fs.readFile('file.txt', 'utf8', function(err, data) { console.log(data); // Could be undefined if error! }); // ✅ CORRECT: Always check error first fs.readFile('file.txt', 'utf8', function(err, data) { if (err) { console.error('Failed:', err); return; } console.log(data); // Safe to use });
Always check if error exists before accessing the result
Common Mistakes
- Not checking the error before using the result
- Passing non-Error objects as the first parameter
- Forgetting to return after handling error
- Checking result before error
Interview Tips
- Know this is the Node.js standard pattern
- Always check error first, return early on error
- Understand why this pattern exists (consistent error handling)
- Know that Promises have largely replaced this in modern code