@soluzioni-futura/retry
v2.0.1
Published
A simple, dependency free, retry module.
Downloads
9
Keywords
Readme
@soluzioni-futura/retry
A simple, dependency free, retry module.
Usage
In the following example, the Axios get function has been wrapped into a function that will be retried up to 3 times with a delay of 1000 ms between each retry. If the Axios call throws with a 500 status code the retry function will stop retrying and throws immediately the error.
import { retry, wait } from "@soluzioni-futura/retry"
import axios, { AxiosError } from "axios"
const shouldThrowFn = (err: AxiosError): boolean {
return err.status === "500"
}
const options = {
retryTimes: 3,
retryDelay: 1000,
shouldThrowFn: (err: AxiosError): boolean => err.status === "500"
}
retry(() => axios.get("https://soluzionifutura.it"), options)
.then(res => console.log(res.status))
.catch(axiosError => console.error(axiosError.status))
Available options
{
retryTimes: number // number of maxium retries; default 3
retryDelay: number // milliseconds beteween each retry; default 500
shouldThrowFn: undefined | (err: any) => boolean // shouldThrowFn function is called each time a retry fails with an error; if true is returned the retry loop will immediately break throwing the error
}