第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

處理Promise.all中的錯誤

處理Promise.all中的錯誤

慕的地6264312 2019-08-26 19:17:47
處理Promise.all中的錯誤我有一個Promise數(shù)組,我正在使用Promise.all(arrayOfPromises)解析;我接著繼續(xù)承諾鏈??雌饋硐襁@樣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,以防它出錯。但是當(dāng)我嘗試時,Promise.all返回它找到的第一個錯誤(忽略其余的),然后我無法從其余的數(shù)據(jù)中獲取數(shù)據(jù)數(shù)組中的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服務(wù)器鏈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調(diào)用(route.async調(diào)用)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中的任何錯誤,然后將整個數(shù)組返回到下一個。然后謝謝!
查看完整描述

3 回答

?
斯蒂芬大帝

TA貢獻(xiàn)1827條經(jīng)驗(yàn) 獲得超8個贊

Promise.all是全有或全無。它會在陣列中的所有承諾解析后解析,或者在其中一個承諾拒絕后立即拒絕。換句話說,它可以使用所有已解析值的數(shù)組進(jìn)行解析,也可以使用單個錯誤進(jìn)行拒絕。


有些庫有一些叫做的東西Promise.when,據(jù)我所知,它會等待陣列中的所有承諾解析或拒絕,但我不熟悉它,而且它不在ES6中。


你的代碼


我同意其他人的意見,你的修復(fù)應(yīng)該有效。它應(yīng)該使用可能包含成功值和錯誤對象的數(shù)組的數(shù)組來解析。在成功路徑中傳遞錯誤對象是不尋常的,但假設(shè)您的代碼期望它們,我認(rèn)為沒有問題。


我能想到為什么它“無法解決”的唯一原因是它沒有顯示我們沒有向你展示的代碼,以及你沒有看到任何關(guān)于這個的錯誤消息的原因是因?yàn)檫@個承諾鏈沒有終止趕上(至于你向我們展示的東西)。


我冒昧地將你的例子中的“現(xiàn)有鏈”分解出來并用一個捕獲來終止鏈。這可能不適合你,但對于閱讀本文的人來說,重要的是始終返回或終止鏈,或者潛在的錯誤,甚至是編碼錯誤,都會被隱藏(這是我懷疑在這里發(fā)生的事情):


Promise.all(state.routes.map(function(route) {

  return route.handler.promiseHandler().catch(function(err) {

    return err;

  });

}))

.then(function(arrayOfValuesOrErrors) {

  // handling of my array containing values and/or errors. 

})

.catch(function(err) {

  console.log(err.message); // some coding error in handling happened

});


查看完整回答
反對 回復(fù) 2019-08-26
?
守著星空守著你

TA貢獻(xiàn)1799條經(jīng)驗(yàn) 獲得超8個贊

為了繼續(xù)Promise.all循環(huán)(即使Promise拒絕),我寫了一個被調(diào)用的實(shí)用函數(shù)executeAllPromises。此實(shí)用程序函數(shù)返回帶有results和的對象errors。

我們的想法是,你傳遞給的所有executeAllPromisesPromise都將被包裝成一個永遠(yuǎn)解決的新Promise。新的Promise解決了一個有2個點(diǎn)的陣列。第一個點(diǎn)保存解析值(如果有的話),第二個點(diǎn)保留錯誤(如果包裝的Promise拒絕)。

作為最后一步,executeAllPromises累積包裝的promises的所有值,并返回帶有數(shù)組的最終對象results和數(shù)組errors

這是代碼:

function executeAllPromises(promises) {
  // Wrap all Promises in a Promise that will always "resolve"
  var resolvingPromises = promises.map(function(promise) {
    return new Promise(function(resolve) {
      var payload = new Array(2);
      promise.then(function(result) {
          payload[0] = result;
        })
        .catch(function(error) {
          payload[1] = error;
        })
        .then(function() {
          /* 
           * The wrapped Promise returns an array:
           * The first position in the array holds the result (if any)
           * The second position in the array holds the error (if any)
           */
          resolve(payload);
        });
    });
  });

  var errors = [];
  var results = [];

  // Execute all wrapped Promises
  return Promise.all(resolvingPromises)
    .then(function(items) {
      items.forEach(function(payload) {
        if (payload[1]) {
          errors.push(payload[1]);
        } else {
          results.push(payload[0]);
        }
      });

      return {
        errors: errors,
        results: results      };
    });}var myPromises = [
  Promise.resolve(1),
  Promise.resolve(2),
  Promise.reject(new Error('3')),
  Promise.resolve(4),
  Promise.reject(new Error('5'))];executeAllPromises(myPromises).then(function(items) {
  // Result
  var errors = items.errors.map(function(error) {
    return error.message  }).join(',');
  var results = items.results.join(',');
  
  console.log(`Executed all ${myPromises.length} Promises:`);
  console.log(`— ${items.results.length} Promises were successful: ${results}`);
  console.log(`— ${items.errors.length} Promises failed: ${errors}`);});


查看完整回答
反對 回復(fù) 2019-08-26
  • 3 回答
  • 0 關(guān)注
  • 1248 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號