Problem understanding callback hell in JavaScript
function task1(callback) {
setTimeout(function() {
console.log("Task 1 complete");
callback(); // calling task2 when task1 finishes
}, 1000); // 1-second delay
}
function task2(callback) {
setTimeout(function() {
console.log("Task 2 complete");
callback(); // calling task3 when task2 finishes
}, 1000); // 1-second delay
}
function task3(callback) {
setTimeout(function() {
console.log("Task 3 complete");
callback(); // calling task4 when task3 finishes
}, 1000); // 1-second delay
}
function task4() {
setTimeout(function() {
console.log("Task 4 complete");
}, 1000); // 1-second delay
}
// Execute the tasks in sequence
task1(function() {
task2(function() {
task3(function() {
task4(); // no more callbacks needed
});
});
});function task1(callback) {
setTimeout(function() {
console.log("Task 1 complete");
callback(); // calling task2 when task1 finishes
}, 1000); // 1-second delay
}
function task2(callback) {
setTimeout(function() {
console.log("Task 2 complete");
callback(); // calling task3 when task2 finishes
}, 1000); // 1-second delay
}
function task3(callback) {
setTimeout(function() {
console.log("Task 3 complete");
callback(); // calling task4 when task3 finishes
}, 1000); // 1-second delay
}
function task4() {
setTimeout(function() {
console.log("Task 4 complete");
}, 1000); // 1-second delay
}
// Execute the tasks in sequence
task1(function() {
task2(function() {
task3(function() {
task4(); // no more callbacks needed
});
});
});Hello guys, I'm not being able to understand this syntax:
// Execute the tasks in sequence
task1(function() {
task2(function() {
task3(function() {
task4(); // no more callbacks needed
});
});
});// Execute the tasks in sequence
task1(function() {
task2(function() {
task3(function() {
task4(); // no more callbacks needed
});
});
});The thing is task1 takes only 1 callbacks, can't we just do something like that: task1(task2) ?
We are we nesting the callbacks I don't understand
