Can someone explain why this works (i.e. delay 1 executes before delay 2):
function testCallBack() {
one(two);
}
function one(callback) {
setTimeout(function () {
alert("delay 1");
callback();
}, 2000);
}
function two() {
setTimeout(function () {
alert("delay 2")
}, 1000);
}
testCallBack();
but this doesn't (delay 2 somehow executes before delay 1):
function testCallBack() {
one(two(three));
}
function one(callback) {
setTimeout(function () {
alert("delay 1");
callback();
}, 3000);
}
function two(callback) {
setTimeout(function () {
alert("delay 2");
callback();
}, 2000);
}
function three(){
setTimeout(function () {
alert("delay 3");
}, 1000);
}
testCallBack();
https://jsfiddle.net/511nLm95/
Am I not nesting my callback functions correctly?