【问题标题】:Calling then twice that conains response on axios然后调用两次包含对 axios 的响应
【发布时间】:2023-01-17 05:01:56
【问题描述】:

如何使用相同的响应对象调用两次?

function a() {
  return axios.get('/foo').then(function(resp){
    // do something with resp, eg. if 401 then force local state to logout
  })
}

// the caller:
a().then(function(resp) {
  // not called, I want same resp, not just data
}).catch(console.log).then(clearLoadingBar)

【问题讨论】:

    标签: javascript axios promise chaining


    【解决方案1】:

    如果你希望你的承诺返回resp,你需要返回它:

    return axios.get('/foo').then(function(resp){
      // do something with resp, eg. if 401 then force local state to logout
      return resp;
    });
    

    【讨论】:

      猜你喜欢
      • 2020-04-28
      • 2017-08-25
      • 2022-01-15
      • 2016-04-03
      • 2019-08-05
      • 1970-01-01
      • 1970-01-01
      • 2019-05-01
      • 1970-01-01
      相关资源
      最近更新 更多