daily pastebin goal
12%
SHARE
TWEET

Untitled

a guest Mar 19th, 2019 50 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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))
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top