Cancel / Stop further requests in axios interceptor on a condition

Viewed 202

Is there any way to cancel all pending axios requests on a specific condition. Supposedly I need to check first if the token expiration is near and I want to stop sending requests while waiting for the new token

Here is my code:

apiClientBook.interceptors.request.use(config => {

      var decoded = jwt_decode(store.getState().auth.user.accessToken);
      var now = new Date();
      var time = now.getTime();
      var exp = new Date((decoded.exp * 1000) - (6 * 1000));
      
      if (time > exp) {
            /// I want to stop/cancel all further request that access this axios instance
    
            return  store.dispatch(getUser()).then((response)=>{
            
            /// Then continue accepting response again after receiving a response here and 
            continue the last accepted request 
           
            console.log(response.payload.accessToken) 
            config.headers.authorization = `Bearer ${store.getState().auth.user.accessToken}`
            return config
        });
      } else{

          config.headers.authorization = `Bearer ${store.getState().auth.user.accessToken}`
          return config
      }    
     
})

0 Answers
Related