Created
December 10, 2021 10:50
-
-
Save kingisaac95/20a3660253ab1ce58268d825325fe1fb to your computer and use it in GitHub Desktop.
Retry request with axios typings
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { AxiosResponse } from 'axios'; | |
import { logger } from 'firebase-functions'; | |
const retryPromise = ( | |
fn: () => Promise<AxiosResponse>, | |
retries = 2, | |
err: null | unknown = null | |
): Promise<AxiosResponse> => { | |
logger.log('Retries: ', retries); | |
if (!retries) { | |
logger.log('Done Retrying...'); | |
return Promise.reject(err); | |
} | |
return fn().catch((err) => { | |
logger.log('Retrying...'); | |
return retryPromise(fn, retries - 1, err); | |
}); | |
}; | |
export default retryPromise; | |
// Thanks to https://stackoverflow.com/a/51332115/6608075 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment