Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const withMaxAttemptsAndBackoff = ({
- waitTime = 100,
- maxAttempts = 3,
- }) => promise => (...args) => {
- if (maxAttempts < 2) return promise(...args)
- return promise(...args).catch(_ =>
- wait(waitTime).then(() =>
- withMaxAttemptsAndBackoff({
- waitTime: Math.min(waitTime * 2, 5000),
- maxAttempts: maxAttempts - 1,
- })(promise)(...args)
- )
- )
- }
- //usage
- const getRecord = (id) => dbClient.get({id})
- const waitForRecord = withMaxAttemptsAndBackoff({maxAttempts:10})(getRecord)
- return waitForRecord(4).then(record => expect(record).to.eql(testRecord))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement