四个使用promise的语句之间的不同点在哪儿?

时间:2024-10-31 08:05:37

http://jsbin.com/tuqukakawo/1/edit?js,console,output

#1

doSomething().then(function () {
return doSomethingElse();
}).then(finalHandler);

答案:

doSomething
|-----------------|
doSomethingElse(undefined)
|------------------|
finalHandler(resultOfDoSomethingElse)
|------------------|

#2

doSomething().then(function () {
doSomethingElse();
}).then(finalHandler);

答案:

doSomething
|-----------------|
doSomethingElse(undefined)
|------------------|
finalHandler(undefined)
|------------------|

#3

doSomething().then(doSomethingElse())
.then(finalHandler);

答案

doSomething
|-----------------|
doSomethingElse(undefined)
|---------------------------------|
finalHandler(resultOfDoSomething)
|------------------|

#4

doSomething().then(doSomethingElse)
.then(finalHandler);

答案

doSomething
|-----------------|
doSomethingElse(resultOfDoSomething)
|------------------|
finalHandler(resultOfDoSomethingElse)
|------------------|