以下Promise行为符合预期,Promise在return语句中得到满足,then之后在最后一次调用之前执行:
(new Promise(resolve => resolve(true)))
.then(function(){
console.log(0);
return new Promise(resolve => setTimeout(resolve, 1000))
.then(function() {
console.log(1);
});
}).then(function() {
console.log(2);
});
Run Code Online (Sandbox Code Playgroud)
结果是
0
1
2
但是当第一个Promise是jQuery时,Promise就像在下面的例子中一样
$.post("index.php")
.then(function() {
console.log(0);
return new Promise(resolve => setTimeout(resolve, 1000))
.then(function() {
console.log(1);
});
}).then(function() {
console.log(2);
});
Run Code Online (Sandbox Code Playgroud)
结果是
0 2 1
这表明第二个Promise没有像标准的JavaScript承诺那样传递.
有没有办法强制标准行为?
我正在使用jQuery 2.0.0.