What is callback hell in Node.js ?
Last Updated :
08 Jan, 2025
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. So, all other processes are put on hold while an operation is executed.
What is Asynchronous Javascript?
- Some functions in Javascript require AJAX because the response from some functions is not immediate. It takes some time and the next operation cannot start immediately. It has to wait for the function to finish in the background. In such cases, the callbacks need to be asynchronous.
- Some external Javascript Web APIs allow AJAX - Asynchronous Javascript and XML. In AJAX, many operations can be performed simultaneously.
What is a callback?
- Callbacks are nothing but functions that take some time to produce a result.
- Usually, these async callbacks (async short for asynchronous) are used for accessing values from databases, downloading images, reading files, etc.
- As these take time to finish, we can neither proceed to the next line because it might throw an error saying unavailable nor we can pause our program.
- So we need to store the result and call back when it is complete.
What is callback hell?
This is a big issue caused by coding with complex nested callbacks. Here, each and every callback takes an argument that is a result of the previous callbacks. In this manner, The code structure looks like a pyramid, making it difficult to read and maintain. Also, if there is an error in one function, then all other functions get affected.
Example: This is an example of typical callback hell.
javascript
app.get("/details", function (req, res) {
const name = req.query.name;
console.log(name);
Scopus.find({ name: name },
{ '_id': 0, 'authorId': 1 },
function (err, result) {
if (err) { }
else {
let searchResult = result[0]["authorId"];
console.log(searchResult);
let options = {
url: "https://api.elsevier.com/content/author/author_id/"
+ searchResult + "?apiKey",
headers: { 'Accept': 'application/json' }
};
request(options, function (error, response, body) {
if (error) {
// Print the error if one occurred
console.error('error in Authors :', error);
// Print the response status code if a response was received
console.log('statusCode:', response && response.statusCode);
res.send("error")
}
else if (!error) {
let jsonObj = JSON.parse(body);
if (jsonObj['author-retrieval-response'] == undefined) {
res.send("No details");
}
else {
let reqData = jsonObj['author-retrieval-response'][0];
let authprofile = reqData["author-profile"]
let names = authprofile["preferred-name"]["indexed-name"]
console.log(names);
let citation = reqData["coredata"]["citation-count"];
let query = { authorId: searchResult };
Scopus.findOneAndUpdate(query, {
name: names,
citationCount: citation
}, function (err, doc, res) {
if (err) {
console.log("error");
}
else {
console.log("success");
}
})
res.render("details", { data: reqData });
}
}
});
}
})
});
You can notice the nested callbacks look like a pyramid which makes it difficult to understand.
How to escape from a callback hell?
- JavaScript provides an easy way of escaping from callback hell. This is done by event queue and promises.
- A promise is a returned object from any asynchronous function, to which callback methods can be added based on the previous function’s result.
- Promises use .then() method to call async callbacks. We can chain as many callbacks as we want and the order is also strictly maintained.
- Promises use .fetch() method to fetch an object from the network. It also uses .catch() method to catch any exception when any block fails.
- So these promises are put in the event queue so that they don't block subsequent JS code. Also once the results are returned, the event queue finishes its operations.
- There are also other helpful keywords and methods like async, wait, set timeout() to simplify and make better use of callbacks.
Similar Reads
What is Callback Hell in JavaScript ? One of the primary ways to manage asynchronous operations in JavaScript is through callback functions that execute after a certain operation completes. However, excessive use of callbacks can lead to an issue known as Callback Hell, making code difficult to read, maintain, and debug.What is Callback
4 min read
What is an error-first callback in Node.js ? 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 er
2 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
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
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
What is Chaining in Node.js ? Chaining in Node.js can be achieved using the async npm module. In order to install the async module, we need to run the following script in our directory: npm init npm i async There are two most commonly used methods for chaining functions provided by the async module: parallel(tasks, callback): Th
2 min read
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
Node.js util.callbackify() Method The util.callbackify() method is an inbuilt application programming interface of the util module which is used to run an asynchronous function and get a callback in the node.js.Syntax:Â Â util.callbackify( async_function ) Parameters: This method accepts single parameter as mentioned above and descri
2 min read
What is the Call Stack in JavaScript ? In JavaScript, the Call Stack is an essential concept that helps the JavaScript engine keep track of function execution. It plays a vital role in managing the execution order of functions and determining how the JavaScript program handles function calls.How Does the Call Stack Work?JavaScript operat
4 min read