1. 程式人生 > >Promise.all()錯誤處理

Promise.all()錯誤處理

為了讓一個fetch宕機之後,promise.all的then裡面還能拿到資料,重點就是給每個fetch的promise物件增加容錯處理
這樣promise.all可以分別處理error問題

getLatestJob(context){
      const result1=api.getJobJsonFromShield(context)
        .then(response => {
          context.state.isShieldFetch=false
          return response.json();
        })
        .catch(function(err) {
          context.state.isShieldFetch=true
          return [];
        });
      const result2=api.getJobJson(context)
        .then(response => {
          context.state.isNBUFetch=false
          return response.json();
        })
        .catch(function(err) {
          context.state.isNBUFetch=true
          return [];
        });

      Promise.all([result1, result2])
        .then(([shieldData, nbuData])=>{
          context.commit('mergeList',{"shield":shieldData,"nbuData":nbuData})

        });
    }