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
});
});
});