title: How to handle API calls when session expires?
body: <p>I'm accessing an API where you authorize first, and you get an access token back, which you use in successive calls. At some point (hours later), the access token expires. So I'm caching the token on the server so any users using the web app will initiate API calls using that token.</p>
  
  <p>But when it expires, I do a recursive call after updating the access token. So, for example (in pseudo-JS):</p>
  
  <pre><code>function getDetails (id) {
    data = HTTP.get(url, {params: ...});
  
    if (!data.success) {
      updateToken(function () {
        return getDetails(id);
      });
    } else { /*.. we're good*/ }
  }
  </code></pre>
  
  <p>There would also be a recursion depth check in there too. If there's a better way to do this, I'd love to hear it. Basically:</p>
  
  <ol>
  <li>Call API</li>
  <li>(failure)</li>
  <li>Update token</li>
  <li>Call API again</li>
  </ol>