Node.js - Error Handling Patterns
You have a function that reads user data asynchronously and uses an error-first callback:
How should you call
function fetchUser(id, callback) {
if (id <= 0) {
callback(new Error('Invalid ID'), null);
} else {
setTimeout(() => {
callback(null, { id, name: 'Alice' });
}, 50);
}
}How should you call
fetchUser to correctly handle errors and print the user's name or the error message?