jhurliman/node-rate-limiter

Is it possible to use async function as callback?

duc-anh-tran opened this issue · 1 comments

I used await inside callMyRequestSendingFunction(...). Is it possible to use async function as callback?
As I tried adding async,

while (morePagesAvailable) {
        let response:any = [];
        myRateLimiter.removeTokens(1, async function(err, remainingRequests) {
            response = await fetcher(url);     
       });

       // Add response to the list of responses
}

Just wrap the entire thing with promise:

    return new Promise((resolve, reject) => {
      limiter.removeTokens(1, (err: Error, remaining: number) => {
        if (err) reject(err);
        if (remaining < 0) {
          reject(new RateLimitError("Too many requests"));
        } else {
          fetcher().then(resolve);
        }
      });
    });