title: Cancel request in Axios Instance class
body: <p>I'm working with Redux-(saga) and a polling function. I need to do a request every 1sec. I need to do a new request when I do not receive a response from the endpoint. In this case I think I need to cancel the previous request and do a new xhr attempt to the api endpoint.</p>
  
  <p>All my request are done with the Axios client. I've a class where I separate all my api call's. I've tried multiple examples (see my code below). And then I call the cancelRequest function.</p>
  
  <p>Could someone please help me into the right direction?</p>
  
  <p>Looked into these issues and axios documentation, but non of those could help me :(</p>
  
  <p><a href="https://stackoverflow.com/questions/44852054/cant-cancel-axios-post-request-via-canceltoken">Cant cancel Axios post request via CancelToken</a>
  <a href="https://redux-resource.js.org/recipes/canceling-requests" rel="nofollow noreferrer">https://redux-resource.js.org/recipes/canceling-requests</a></p>
  
  <pre><code>import { getCookie } from '......';
  
  const CancelToken = Axios.CancelToken;
  let cancel;
  
  /**
   * Create a single axios client for the 
   * @type {AxiosInstance}
   */
  const apiClient = apiUrl =&gt;
    Axios.create({
      baseURL: apiUrl,
      cancelToken: new CancelToken(function executor(c) {
        // An executor function receives a cancel function as a parameter
        cancel = c;
      }),
    });
  
  /**
   * The Api is initialised with a Axios client.
   */
  class PinResetApi {
    constructor(client) {
      this.client = client;
    }
  
    /**
     * Reset pin controller
     */
    requestChangePin = () =&gt;
      this.client(......url).request({
        method: 'GET',
        headers: {
          ...
        },
      });
  
    /**
     * Cancel reset pin API
     */
    cancelRequest = () =&gt; this.client.cancel();
  
    /**
     * Reset pin status controller
     * @param
     */
    requestChangePinStatus = transactionId =&gt;
      this.client(.....url).request({
        method: 'GET',
        headers: {
          ...
        },
  
      });
  }
  
  const _api = new PinResetApi(apiClient);
  
  export default _api;
  export { PinResetApi };
  </code></pre>