处理Promise.all中的错误
我有一个Promise数组,我正在使用Promise.all(arrayOfPromises)解析;
我接着继续承诺链。看起来像这样
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();});
我想添加一个catch语句来处理单个promise,以防它出错。但是当我尝试时,Promise.all返回它找到的第一个错误(忽略其余的),然后我无法从其余的数据中获取数据数组中的promise(没有错误)。
我尝试过像......
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();});
但这并没有解决。
谢谢!
-
编辑:
下面的答案完全正确,代码因其他原因而破裂。如果有人有兴趣,这就是我最终得到的解决方案......
Node Express服务器链
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调用(route.async调用)
return async() .then(function(result) { // dispatch a success return result; }) .catch(function(err) { // dispatch a failure and throw error throw err; });
在.then之前放置.catch for Promise.all似乎是为了捕获原始promise中的任何错误,然后将整个数组返回到下一个。然后
谢谢!
斯蒂芬大帝
守着星空守着你
相关分类