I
I
Igor Bezlepkin2018-07-10 15:07:34
Vue.js
Igor Bezlepkin, 2018-07-10 15:07:34

How to make an asynchronous request in vue-resource?

Hey! With 2 ajax requests through vue-resource, 401 is returned when the token expires.

let allowRefresh = true
Vue.http.interceptors.push((request, next) => {
  next((response) => {
    if (allowRefresh && response.status === 401) {
      allowRefresh = false
      store.commit('SET_AUTH_REFRESHING')
      return user.refresh(request).then(result => {
        return Vue.http(request).then(data => {
          return data
        })
      }, () => {
        user.logout()
      })
    }
  })
})

But since the requests go synchronously, the first one after 401 updates the token and continues its request. But the second is simply lost.
5b44a154243c7730186371.png
So, is it possible to somehow stop subsequent requests after the first one until it completes and installs a new token?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question