promise.all 異常處理


promise.all 中任何一個promise 出現錯誤的時候都會執行reject,
導致其它正常返回的數據也無法使用了
有兩個解決方法:
1.全部改為串行調用(失去了node 並發優勢)
2.當promise捕獲到error 的時候,代碼吃掉這個異常,返回resolve,約定特殊格式表示這個調用成功了

var p1 =new Promise(function(resolve,reject){
setTimeout(function(){
resolve(1);
},0)
});
var p2 = new Promise(function(resolve,reject){
setTimeout(function(){
resolve(2);
},200)
});
var p3 = new Promise(function(resolve,reject){
setTimeout(function(){
try{
console.log(XX.BBB);
}
catch(exp){
resolve("error");
}
},100)
});
Promise.all([p1, p2, p3]).then(function (results) {
console.log("success")
console.log(results);
}).catch(function(r){
console.log("err");
console.log(r);
});


點贊 1
收藏
分享

ISaiSai
發布了401 篇原創文章 · 獲贊 130 · 訪問量 242萬+
他的留言板
關注

————————————————
版權聲明:本文為CSDN博主「ISaiSai」的原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/isaisai/article/details/48437091


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM