function task1(callback) {
setTimeout(() => {
console.log('Task 1 completed');
callback();
}, 1000);
}
function task2(callback) {
setTimeout(() => {
console.log('Task 2 completed');
callback();
}, 1000);
}
function task3(callback) {
setTimeout(() => {
console.log('Task 3 completed');
callback();
}, 1000);
}
function executeTasks(tasks, finalCallback) {
function runTask(index) {
if (index >= tasks.length) {
finalCallback();
return;
}
tasks[index](() => {
runTask(index + 1);
});
}
runTask(0);
}
const tasks = [task1, task2, task3];
executeTasks(tasks, () => {
console.log('All tasks completed');
});