Search code examples
javascriptaxiospromisechaining

Calling then twice that contains response on axios


How to call then twice with same response object?

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)

Solution

  • If you want your promise to return resp, you will need return it:

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