title: JS Promise waitFor refresh Token
body: <p>The situation is simple : </p>
  
  <p>I have a nodejs server (called it API-A) that use Bluebird as a Promise handler. 
  I have a client(browser) that ask data through the API-A which get the data from another API (API-B). API-B could be a Weather service from example and then API-A aggregate the data with other data and send it back to client.</p>
  
  <p>The situation is the next: API-B need a token with a TTL of 1800 second. 
  So for each request done by the client, I check if my token is expired or not.</p>
  
  <p>I have this kind of code : </p>
  
  <pre><code>function getActivities()
  {
  return this.requestAuthorisation()
         .then(()=&gt;{
          // some other code that is not interesting
         })
  }
  </code></pre>
  
  <p>Everything works fine with the glory of promise. 
  requestAuthorisation() check if the token is valid (next!!!) and if not (I do a request the API-B to refresh the token)
  The problem is here: 
  between the time, the token is expired and the time to obtain a fresh one, some times happen. if 1000 clients ask at the same time these, I will have 1000 request of token to API-B, that is not cool.</p>
  
  <p>How can I avoid that ? I try to avoid a cron-way to do it, to avoid unnecessary call and the problem is the same. </p>
  
  <p>I try to create a sort of global variable (boolean) that track the refreshing status of the token but impossible to find a sort of Promise.WaitFor (variable change) 
  the Promise.all can not be use because I am in different scope of event.</p>
  
  <p>Is there a way to queue until the token is refresh ?</p>
  
  <p>Please help !</p>