title: Why does axios interceptors go into an infinite loop when trying to update a token?
body: <p>I have <em>access_token</em> and <em>refresh_token</em> in <code>localStorage</code>.</p>
  <p>Before calling my backend, I want to check if the token has expired:</p>
  <pre><code> instance.interceptors.request.use(
      (config) =&gt; {
        const accessToken = localStorage.getItem(&quot;access_token&quot;);
        const auth = jwt_decode(accessToken);
        const expireTime = auth.exp * 1000;
        const now = + new Date();
        if (expireTime &gt; now) {
          console.log(&quot;true&quot;);
          config.headers[&quot;Authorization&quot;] = 'Bearer ' + accessToken;
        } else {
          console.log(&quot;false&quot;);
          (async() =&gt; {
            const refreshToken = localStorage.getItem(&quot;refresh_token&quot;);
            const response = await refreshAccessToken(refreshToken);
            const data = response.data;
            const token = data.accessToken;
            setAuth({token,refreshToken});
            localStorage.setItem(&quot;access_token&quot;, token);
            config.headers[&quot;Authorization&quot;] = 'Bearer ' + token;
          })();        
        }
        return config;
      },
      (error) =&gt; {
        return Promise.reject(error);
      }
    );
  </code></pre>
  <p><em>refresh token method</em></p>
  <pre><code> const refreshAccessToken = async (token) =&gt; {
      return instance.post(&quot;/refresh&quot;, token);
    };
  </code></pre>
  <p>And this code works until I add a line:</p>
  <pre><code>const response = await refreshAccessToken(refreshToken);
  </code></pre>
  <p>After that, when the token expires, the page freezes indefinitely in a repeat of the false branch although I am putting a new token in the storage.</p>
  <p>I will be glad if someone tells me what's wrong, maybe I'm doing something wrong with interceptors?</p>
  <p><em>refresh response</em>:</p>
  <pre><code>{
      &quot;accessToken&quot;: &quot;...&quot;,
      &quot;refreshToken&quot;: &quot;...&quot;
  }
  </code></pre>