// defination
Callback hell is a phenomenon that afflicts a JavaScript developer
when he tries to execute multiple asynchronous operations one after the other.
By nesting callbacks in such a way,
we easily end up with error-prone, hard to read,
and hard to maintain code.Soln: Best code practice to handle it.
//example
const makeBurger = nextStep => {
getBeef(function(beef) {
cookBeef(beef, function(cookedBeef) {
getBuns(function(buns) {
putBeefBetweenBuns(buns, beef, function(burger) {
nextStep(burger);
});
});
});
});
};
sdav
function task1(callback) {
setTimeout(function() {
console.log("Task 1");
callback()
},2000)
}
function task2(callback) {
setTimeout(function() {
console.log("Task 2");
callback()
},2000)
}
function task3(callback) {
setTimeout(function(callback) {
console.log("Task 3");
callback()
},2000)
}
function task4() {
console.log("Task 4");
}
function main() {
task1(function() {
task2(function() {
task3(function() {
task4();
})
})
})
}
main();