Advertisement
Guest User

Untitled

a guest
Mar 19th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.57 KB | None | 0 0
  1. const withMaxAttemptsAndBackoff = ({
  2. waitTime = 100,
  3. maxAttempts = 3,
  4. }) => promise => (...args) => {
  5. if (maxAttempts < 2) return promise(...args)
  6.  
  7. return promise(...args).catch(_ =>
  8. wait(waitTime).then(() =>
  9. withMaxAttemptsAndBackoff({
  10. waitTime: Math.min(waitTime * 2, 5000),
  11. maxAttempts: maxAttempts - 1,
  12. })(promise)(...args)
  13. )
  14. )
  15. }
  16. //usage
  17. const getRecord = (id) => dbClient.get({id})
  18. const waitForRecord = withMaxAttemptsAndBackoff({maxAttempts:10})(getRecord)
  19. return waitForRecord(4).then(record => expect(record).to.eql(testRecord))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement