title: Axios: How to intercept and respond to axios request
body: <p>Is there a way to not just intercept but also respond to an axios request before it has been sent off? As in, send a request from the browser and respond to it from the browser + prevent it from sending request.</p>
  <p>I know that I can use <code>axios interceptors</code> to intercept the request and response before it is sent and  returned to the component and I know that in the request interceptor I can throw an error and trigger the response interceptor with a failed request. How can I do the same for a successful request? Given certain conditions I want axios to respond as if it passed to the server when it actually never made it past the interceptor. is this possible?</p>
  <p>Here's pseudo code for what I've got so far:</p>
  <pre class="lang-js prettyprint-override"><code>      axios.interceptors.request.use(
        request =&gt; {
          if (localResponse) {
                throw { isLocal: true, data: { hello: 'world' } }; // &lt;- this will stop request and trigger 
                                                                   // response error. I want to trigger 
                                                                   // the actual response callback
          } else {
              return request;   // &lt;- will perform full request
          }
        },
        error =&gt; {
          return Promise.reject(error);
        }
      );
  
      axios.interceptors.response.use(
        response =&gt; {
          return response; // &lt;- I want to trigger this callback
        },
        error =&gt; { // &lt;- so far i can only trigger a response error
          if (error?.isLocal) { 
            return Promise.resolve(error.data); 
          }
          
          return Promise.reject(error);
        }
      );
  </code></pre>
  <p>I've tried just resolving the request interceptor but that tries to continue to fulfill the request. Does anyone have any creative ideas for solving this problem? Maybe there's a better solution than using interceptors?</p>