Skip to content

Feature suggestion: automated rate limiting handling #86

Answered by alkihis
SachaG asked this question in Q&A
Discussion options

You must be logged in to vote

You can await when there is a rate limit error, as suggested:

function sleep(ms: number) {
  return new Promise(resolve => setTimeout(ms, resolve));
}

async function autoRetryOnRateLimitError<T>(callback: () => T | Promise<T>) {
  while (true) {
    try {
      return await callback();
    } catch (error) {
      if (error instanceof ApiResponseError && error.rateLimitError && error.rateLimit) {
        const resetTimeout = error.rateLimit.reset * 1000; // convert to ms time instead of seconds time
        const timeToWait = resetTimeout - Date.now();

        await sleep(timeToWait);
        continue;
      }

      throw error;
    }
  }
}

// Get your paginator here
const paginator = a…

Replies: 4 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by alkihis
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #81 on September 22, 2021 16:21.