There Is No Callback Hell In JavaScript
There is no "callback hell" in JavaScript; it is just a bad programming style.
The infamous JavaScript "callback hell" can easily be fixed by unnesting all the callbacks into separate functions.
Here is an example:
const verifyUser = function(username, password, callback) {
dataBase.verifyUser(username, password, function(error, userInfo) {
if (error) {
callback(error);
} else {
dataBase.getRoles(username, function(error, roles) {
if (error) {
callback(error);
} else {
dataBase.logAccess(username, function(error) {
if (error) {
callback(error);
} else {
callback(null, userInfo, roles);
}
})
}
})
}
})
};
The same code with separate functions instead of inline callbacks:
const verifyUser = function(username, password, callback) {
dataBase.verifyUser(username, password, function(error, userInfo) {
if (error) return callback(error);
getRoles(username, userInfo, callback);
})
}
const getRoles = function(username, userInfo, callback) {
dataBase.getRoles(username, function(error, roles) {
if (error) return callback(error);
logAccess(username, userInfo, roles, callback);
})
}
const logAccess = function(username, userInfo, roles, callback) {
dataBase.logAccess(username, function(error) {
if (error) return callback(error);
callback(null, userInfo, roles);
})
}
It is much more readable, manageable and reusable than the approach with inline functions. In fact, there is no "callback hell" here because there are no nested callbacks in this code anymore.
This way, the "callback hell" is simply a bad style of writing code that is easy to avoid.
Libraries like step, async, as well as promises and async/await, all provide a way to manage asynchronous code. These methods may be less verbose or more convenient than "regular" callback functions, but callbacks can work too.