sinclairzx81 / sidewinder

Type Safe Micro Services for Node
Other
61 stars 3 forks source link

Add Async Retry Primitive #34

Closed sinclairzx81 closed 1 year ago

sinclairzx81 commented 1 year ago

This PR adds a simple async Retry primitive to @sidewinder/async. This will run a async function up to a configured attempts threshold and throw last error if over threshold. The Retry.run function will return the inner calls return value on success.

import { Retry } from '@sidewinder/async'

Retry.run(async (attempt) => {
   if(attempt < 10) throw Error('failed')
   return 'success'
}, {
  attempts: 32,
  delay: 1000
}).then(console.log) // 'success'
 .catch(console.log) // last error