【发布时间】:2015-05-29 04:42:45
【问题描述】:
我有许多要按顺序调用的承诺(使用 Q),这是我的代码:
// All the promises are called sequentially
var result = promises.reduce(function(promise, item) {
return promise.then(function () {
var obj = toPush.shift();
});
}, Q());
// Check the result
result.then(function() {
// Do something if all of the promises full
}).catch(function() {
// Do something if ONE of the promise is rejected and stop the others
}).finally(function() {
App.network.stopLoader();
});
promises 是一个 promise 数组(回调函数)
效果很好,所有的承诺都是按顺序完成的,但是当一个承诺被拒绝时,它仍然会进入 then 函数而不是 catch。 为什么?
我用过这个:Break a dynamic sequence of promises with Q
感谢您的帮助:)
【问题讨论】:
-
什么是
promises,什么是toPush?你为什么要改变它,你在用obj做什么?看起来你的顺序代码根本不是异步的。 -
promises 是一个选项卡,其中包含一个返回承诺的函数数组,toPush 是一个选项卡,允许我构造选项卡承诺,如果承诺已解决,则 toPush.shift,否则我需要停止所有其他承诺并保存 toPush。
-
promises数组中的那些函数永远不会真正被调用 - 你不要使用item!我想知道为什么你认为他们执行得很好。 -
我改了,还是不行。
-
怎么改的?请edit你的问题用新代码修改它
标签: javascript promise q