SHARE
TWEET

Untitled

a guest Jul 17th, 2019 62 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const numberOfTries = 5
  2. const delayTime = 500
  3.  
  4. const delay = async () => {
  5.   return new Promise((resolve) => {
  6.     setTimeout(() => resolve(), delayTime)
  7.   })
  8. }
  9. const requestHandler = async (walletFunction) => {
  10.   try {
  11.     for (let i = 0; i < numberOfTries; i++) {
  12.       try {
  13.         const result = await walletFunction()
  14.         return result
  15.       } catch (error) {
  16.         if (error.code) {
  17.           switch (error.code) {
  18.             case 13:
  19.               await delay()
  20.               break
  21.             case 14:
  22.               throw { message: "Can't connect to node IP" }
  23.             default:
  24.               throw error
  25.           }
  26.         }
  27.       }
  28.     }
  29.     throw { message: "Can't perform transaction right now. Try again in a few minutes" }
  30.   } catch (error) {
  31.     throw error
  32.   }
  33. }
  34.  
  35. module.exports = requestHandler
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
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top