Complete the code to define a callback function that logs a message.
function greet(name, [1]) { console.log('Hello ' + name); [1](); }
The callback function is passed as an argument and called inside the main function.
Complete the code to call a function with a callback that logs 'Done'.
function doTask(cb) {
setTimeout(() => {
console.log('Task complete');
[1]();
}, 1000);
}
doTask(() => console.log('Done'));The callback function passed as 'cb' is called after the task finishes.
Fix the error in the nested callbacks to avoid callback hell.
function step1(cb) {
setTimeout(() => {
console.log('Step 1');
cb();
}, 500);
}
function step2(cb) {
setTimeout(() => {
console.log('Step 2');
cb();
}, 500);
}
step1(() => {
[1](() => {
console.log('Step 3');
});
});To avoid callback hell, call the next step function properly inside the previous callback.
Fill both blanks to create a nested callback structure that logs steps in order.
function first(cb) {
setTimeout(() => {
console.log('First');
[1]();
}, 300);
}
function second(cb) {
setTimeout(() => {
console.log('Second');
[2]();
}, 300);
}
first(() => {
second(() => {
console.log('Done');
});
});Both functions call their callback parameter to continue the chain.
Fill all three blanks to create a callback chain that logs three steps in order.
function one(cb) {
setTimeout(() => {
console.log('One');
[1]();
}, 200);
}
function two(cb) {
setTimeout(() => {
console.log('Two');
[2]();
}, 200);
}
function three(cb) {
setTimeout(() => {
console.log('Three');
[3]();
}, 200);
}
one(() => {
two(() => {
three(() => {
console.log('All done');
});
});
});Each function calls its callback parameter to continue the chain.
