Promise based retry and backoff, bring your own Promises
MIT License
Promise based retry and backoff, bring your own Promises
npm install u-promised
import { get } from 'highwire'
import { retry } from 'u-promised'
const fetchGoogle = () => get('http://google.com')
retry(3, fetchGoogle)
.then((google) => console.log(google))
.catch((err) => console.log('Oops, 4 attempts total', err))
// retry forever - no catch
retry(-1, fetchGoogle)
.then((google) => console.log(google))
// evalute retries
const evalRetry = () => {
let index = 0
return () => {
if (index === 5) return false
index++
return true
}
}
retry(evalRetry(), fetchGoogle)
.then((google) => console.log(google))
.catch((err) => console.log('Oops, 6 attempts total', err))
import { get } from 'highwire'
import { backoff } from 'u-promised'
const fetchGoogle = () => get('http://google.com')
const SECOND = 1000
backoff(SECOND, SECOND, 3, fetchGoogle)
.then((google) => console.log(google))
.catch((err) => console.log('Oops, 4 attempts total', err))
// backoff forever
backoff(SECOND, SECOND, -1, fetchGoogle)
.then((google) => console.log(google))
// evaluate backoff
const evalBackoff = () => {
let index = 0
return () => {
if (index === 5) return false
index++
return true
}
}
backoff(SECOND, SECOND, evalBackoff(), fetchGoogle)
.then((google) => console.log(google))
.catch((err) => console.log('Oops, 6 attempts total', err))
There's really not much to this codebase. Just run npm test
. If you would like to contribute, please open an issue explaining what you would like to change/fix/add and make a pull request.