1, 2, 3, 4
,但实际输出顺序为1, 4, 3, 2
。self.promiseChain = new Promise(function (resolve, reject) {
setTimeout(resolve, 4000);
}).then(function () {
console.log(1);
});
self.promiseChain.then(function () {
return new Promise(function (resolve, reject) {
setTimeout(resolve, 3000);
}).then(function () {
console.log(2);
});
});
self.promiseChain.then(function () {
return new Promise(function (resolve, reject) {
setTimeout(resolve, 2000);
}).then(function () {
console.log(3);
});
});
self.promiseChain.then(function () {
return new Promise(function (resolve, reject) {
setTimeout(resolve, 200);
}).then(function () {
console.log(4);
});
});
http://www.es6fiddle.net/imu5bhoj/
我读到的所有有关Promise的内容都表明在这样的'flat'链条中应该可以实现所需的顺序。 显然我错过了一些细节? 有人能帮忙指点一下吗?
这里有一个fiddle(http://www.es6fiddle.net/imu6vh1o/),介绍了如何以非平面方式解决此问题,但很难理解,并使连续链接变得笨拙。
我在stackoverflow上搜索了类似的问题,但没有找到使用简单明了的例子来通用回答这个问题。
self.promiseChain = self.promiseChain.then...
? - elclanrs