[Feature Request] Add Promise Support
huan opened this issue ยท 6 comments
huan commented
Suppor using Promise instead of CallBack will be very convenient.
If you agree, I'd like to make a Pull Request for it.
MikeyBurkman commented
The p-retry library uses this library internally and allows for retrying of promises with roughly the same options this one does. Might be worth giving that a shot.
squidfunk commented
Seems to work with Promises:
export function retryable<T>(
action: () => Promise<T>,
options: OperationOptions
): Promise<T> {
return new Promise((resolve, reject) => {
const op = operation(options)
op.attempt(async () => {
try {
resolve(await action())
} catch (err) {
if (!op.retry(err))
reject(err)
}
})
})
}
tangyong204 commented
Promise instead of CallBack will be very convenient.I agree
Morikko commented
It works out of the box ๐
const retry = require('retry')
const isItGood = [false, false, true]
let attempt_nbr = 0
async function wait() {
return new Promise(res => setTimeout(res, 2000))
}
async function retryer() {
let operation = retry.operation()
return new Promise((resolve, reject)=>{
operation.attempt(async function(currentAttempt) {
console.log('Attempt nbr:', attempt_nbr)
await wait()
console.log('...waited over')
const err = !isItGood[attempt_nbr] ? true : null
if (operation.retry(err)) {
attempt_nbr++
return
}
if (isItGood[attempt_nbr]) resolve('Bueno')
else reject(operation.mainError())
})
})
}
async function main() {
console.log('Start')
await retryer()
console.log('End')
}
main()
ctesniere commented
Maybe add an example in the README.md file with new Promise((resolve, reject) => { ... } );
?