What is an error-first callback in Node.js ?
Last Updated :
07 Mar, 2022
In this article, we are going to explore the Error-first callback in Node.js and its uses. Error-first callback in Node.js is a function that returns an error object whenever any successful data is returned by the function.
The first argument is reserved for the error object by the function. This error object is returned by the first argument whenever any error occurs during the execution of the function.
The second argument is reserved for any type of successful data returned by the function. The error object is set to null when no error occurs.
The below example and steps show the implementation of the Error-first callback:
Step 1: Create a file with the name index.js.
Step 2: Add the fs module import in this module
Step 3: Once the module is imported, we will be implementing the error-first callback function on this method using the fs module. The fs module can be used through the following statement in index.js
const fs = require("fs");
Use the following command to execute the index.js file.
node index.js
In the below example, we will be using the fs.readFile() method for showing the use of the error-first callback function.
Example 1:Â
JavaScript
// Using the fs module through import
const fs = require("fs");
// The following file does not exists
const file = "file.txt";
// This should throw an error
// using the Error-first callback
const ErrorFirstCallback = (err, data) => {
if (err) {
return console.log("Error: " + err);
}
console.log("Function successfully executed");
};
// Executing the function
fs.readFile(file, ErrorFirstCallback);
Output:
Example 2:
JavaScript
// Using the fs module through import
const fs = require("fs");
// This file exists in the system
const file = "file.txt";
// Calling the function to read file
// with error callback and data
const ErrorFirstCallback = (err, data) => {
if (err) {
return console.log(err);
}
console.log("Function successfully executed");
console.log("File Content : " + data.toString());
};
// Executing the function
fs.readFile(file, ErrorFirstCallback);
Output:
Similar Reads
Error-First Callback in Node.js Error-First Callback in Node.js is a function which either returns an error object or any successful data returned by the function. The first argument in the function is reserved for the error object. If any error has occurred during the execution of the function, it will be returned by the first ar
2 min read
What is callback hell in Node.js ? To know what is callback hell, we have to start with Synchronous and Asynchronous Javascript. What is Synchronous Javascript? In Synchronous Javascript, when we run the code, the result is returned as soon as the browser can do. Only one operation can happen at a time because it is single-threaded.
3 min read
What is a callback function in Node? In the context of NodeJS, a callback function is a function that is passed as an argument to another function and is executed after the completion of a specific task or operation. Callbacks are fundamental to the asynchronous nature of NodeJS, allowing for non-blocking operations and enabling effici
2 min read
What is Callback Hell and How to Avoid it in NodeJS? In NodeJS, asynchronous programming can lead to Callback Hell, where deeply nested callbacks make the code hard to read and maintain. This happens when multiple asynchronous operations are chained together, creating a complex structure that's difficult to manage. Callback Hell in NodeJSCallback Hell
6 min read
How to Avoid Callback Hell in Node.js ? Callback hell, often referred to as "Pyramid of Doom," occurs in Node.js when multiple nested callbacks lead to code that is hard to read, maintain, and debug. This situation arises when each asynchronous operation depends on the completion of the previous one, resulting in deeply nested callback fu
3 min read