q.all 是否有任何替代方案,因为回调
q.all().then()
仅当所有 http 调用成功时才会被调用。
即使一次http调用失败,也会进入.catch
部分。
有什么方法可以让它进入 q.all().then()
部分,即使很少的 http 调用失败?
最佳答案
如果您实际上谈论的是 Q
库,那么 Q.allSettled()
正是为了这个目的:
Q.allSettled([promise1, promise2]).then(function (results) {
// results is an array of objects of the form:
// { state: "fulfilled", value: ... }
// or
// { state: "rejected", reason: ... }
});
如果您谈论的是 AngularJS 中包含的 $q
,它没有此方法,但您可以制作自己的临时版本:
myApp.service('allSettled', ['$q', function ($q) {
return function (values) {
return $q.all(values.map(function (value) {
return $q.resolve(value)
.then(function (result) {
return { state: "fulfilled", value: result };
})
.catch(function (error) {
return { state: "rejected", reason: error };
});
}));
}
}]);
关于angularjs - angualarJs 或 node.js 中 q.all 的替代方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43319853/