I have doSomething
, doSomethingElse
& finalHandler
functions as following:
function doSomething() {
console.log('doSomething(): start');
return new Promise(function (resolve) {
setTimeout(function () {
console.log('doSomething(): end');
resolve();
}, 1000);
});
}
function doSomethingElse() {
console.log('doSomethingElse(): start');
return new Promise(function (resolve) {
setTimeout(function () {
console.log('doSomethingElse(): end');
resolve();
}, 1000);
});
}
function finalHandler() {
console.log('finalHandler(): start');
return new Promise(function (resolve) {
setTimeout(function () {
console.log('finalHandler(): end');
resolve();
}, 1000);
});
}
I have a function example1
which looks like this:
function example1() {
doSomething().then(function () {
return doSomethingElse();
}).then(finalHandler);
}
is returning
doSomething(): start
doSomething(): end
doSomethingElse(): start
doSomethingElse(): end
finalHandler(): start
finalHandler(): end
I have example2
:
function example2() {
doSomething().then(function () {
doSomethingElse();
}).then(finalHandler);
}
returning:
doSomething(): start
doSomething(): end
doSomethingElse(): start
finalHandler(): start
doSomethingElse(): end
finalHandler(): end
And example3
:
function example3() {
doSomething().then(doSomethingElse())
.then(finalHandler);
}
returning:
doSomething(): start
doSomethingElse(): start
doSomething(): end
finalHandler(): start
doSomethingElse(): end
finalHandler(): end
And example4
:
function example4() {
doSomething().then(doSomethingElse)
.then(finalHandler);
}
returning:
doSomething(): start
doSomething(): end
doSomethingElse(): start
doSomethingElse(): end
finalHandler(): start
finalHandler(): end
I can understand that different function calling syntax is giving different results - but what is the concept behind this behavior?
I am not looking for a correction or optimized code. I wanted to know how is the flow navigated with different syntax?