I have an array of Promises that I'm resolving with Promise.all(arrayOfPromises);
(我有一个用Promise.all(arrayOfPromises);
解决的Promises数组Promise.all(arrayOfPromises);
)
I go on to continue the promise chain.
(我继续继续诺言链。)
Looks something like this(看起来像这样)
existingPromiseChain = existingPromiseChain.then(function() {
var arrayOfPromises = state.routes.map(function(route){
return route.handler.promiseHandler();
});
return Promise.all(arrayOfPromises)
});
existingPromiseChain = existingPromiseChain.then(function(arrayResolved) {
// do stuff with my array of resolved promises, eventually ending with a res.send();
});
I want to add a catch statement to handle an individual promise in case it errors, but when I try, Promise.all
returns the first error it finds (disregards the rest), and then I can't get the data from the rest of the promises in the array (that didn't error).
(我想添加一个catch语句来处理单个promise,以防万一出错,但是当我尝试时, Promise.all
返回它发现的第一个错误(忽略其余错误),然后我无法从其余错误中获取数据数组中的promise(没有错误)。)
I've tried doing something like ..
(我尝试做类似..)
existingPromiseChain = existingPromiseChain.then(function() {
var arrayOfPromises = state.routes.map(function(route){
return route.handler.promiseHandler()
.then(function(data) {
return data;
})
.catch(function(err) {
return err
});
});
return Promise.all(arrayOfPromises)
});
existingPromiseChain = existingPromiseChain.then(function(arrayResolved) {
// do stuff with my array of resolved promises, eventually ending with a res.send();
});
But that doesn't resolve.
(但这并不能解决。)
Thanks!
(谢谢!)
--
(-)
Edit:
(编辑:)
What the answers below said were completely true, the code was breaking due to other reasons.
(下面的答案完全正确,但代码由于其他原因而中断。)
In case anyone is interested, this is the solution I ended up with ...(如果有人感兴趣,这就是我最终得到的解决方案...)
Node Express Server Chain
(节点快速服务器链)
serverSidePromiseChain
.then(function(AppRouter) {
var arrayOfPromises = state.routes.map(function(route) {
return route.async();
});
Promise.all(arrayOfPromises)
.catch(function(err) {
// log that I have an error, return the entire array;
console.log('A promise failed to resolve', err);
return arrayOfPromises;
})
.then(function(arrayOfPromises) {
// full array of resolved promises;
})
};
API Call (route.async call)
(API调用(route.async调用))
return async()
.then(function(result) {
// dispatch a success
return result;
})
.catch(function(err) {
// dispatch a failure and throw error
throw err;
});
Putting the .catch
for Promise.all
before the .then
seems to have served the purpose of catching any errors from the original promises, but then returning the entire array to the next .then
(把.catch
为Promise.all
的之前.then
似乎已经担任了从原来的承诺捕捉任何错误,但随后返回整个数组的下一个目的.then
)
Thanks!
(谢谢!)
ask by Jon translate from so