Skip to content

Instantly share code, notes, and snippets.

@Kamshak Kamshak/retry.ts
Created Aug 8, 2019

Embed
What would you like to do?
Retry rate limited request on node using bluebird-retry
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'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.