Solution to https://twitter.com/nolanlawson/status/578948854411878400.
doSomething().then(function () {
return doSomethingElse();
}).then(finalHandler);
Answer:
doSomething
|-----------------|
doSomethingElse(undefined)
|------------------|
finalHandler(resultOfDoSomethingElse)
|------------------|
doSomething().then(function () {
doSomethingElse();
}).then(finalHandler);
Answer:
doSomething
|-----------------|
doSomethingElse(undefined)
|------------------|
finalHandler(undefined)
|------------------|
doSomething().then(doSomethingElse())
.then(finalHandler);
Answer:
doSomething
|-----------------|
doSomethingElse(undefined)
|---------------------------------|
finalHandler(resultOfDoSomething)
|------------------|
doSomething().then(doSomethingElse)
.then(finalHandler);
Answer:
doSomething
|-----------------|
doSomethingElse(resultOfDoSomething)
|------------------|
finalHandler(resultOfDoSomethingElse)
|------------------|
It's a bit hard to tell, because
doSomethingElse
isn't defined.In #1 and #2 it looks like we assume that it's a function that returns a promise. And in #2 we learn the lesson of not passing a promise back at the end of all callbacks passed to
then()
.In #3, if it returns a function that returns a promise, then its chart will look like the chart for #4.
In #4 we are again assuming that it's a function that returns a promise, otherwise
doSomethingElse
andfinalHandler
will be overlapped.Whew!