title: Javascript: Rate limit but continue execution when limit is over
body: <p>I have something like this in Node.js and I'm using the <a href="https://www.npmjs.com/package/limiter" rel="nofollow noreferrer">Limiter</a> to limit how many times this function can be executed in a minute. (10 times per min for example)</p>
  
  <pre><code>  this.queue = [];
    this.limiter = new RateLimiter(10, 60000);
    someFunction() {
             for(var element of this.queue) {
                     this.limiter.removeTokens(1, (err, remainingRequests) =&gt; {
                           //do something with element in queue and remove it from queue
                      });
              }
    }
  </code></pre>
  
  <p>Now I want this function to run immediately after something added to the queue, if it hits the limit, it should wait until the tokens reset and then continue executing for the remaining items in the queue. Keep in mind that items can be added to the queue all the time (assume 2 items added to the queue every sec all the time for example ). What is the best way to achieve these results? Is there a better implementation for this or any available packages that would be helpful? </p>