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})

        });
    }

猜你喜欢

转载自blog.csdn.net/weixin_36869329/article/details/82853995