Skip to content

Instantly share code, notes, and snippets.

@ValentinFunk
Created August 8, 2019 12:25
Show Gist options
  • Save ValentinFunk/acb13654acdc4e07efa7d27de2146d01 to your computer and use it in GitHub Desktop.
Save ValentinFunk/acb13654acdc4e07efa7d27de2146d01 to your computer and use it in GitHub Desktop.

Revisions

  1. ValentinFunk created this gist Aug 8, 2019.
    19 changes: 19 additions & 0 deletions retry.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@

    import * as retry from 'bluebird-retry';
    function isRateLimitOrTempError(err: Error & { statusCode?: number }) {
    return err.statusCode && (
    err.statusCode == 429 // Rate Limit
    || err.statusCode == 502 // Temporary error, retry again
    );
    }
    function retryOnRateLimited<T>(request: () => Promise<T>): Promise<T> {
    return retry(request, {
    max_tries: 50,
    backoff: 1.25,
    throw_original: true,
    interval: 2000,
    predicate: isRateLimitOrTempError,
    });
    }

    const result = await retryOnRateLimited(() => request.post('/limit-me'))