Created
January 16, 2018 14:50
-
-
Save think2011/8f8a7a5f3279c8d6bbd02de3aa71eb45 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function retry(func, interval = 1000, times = 6) { | |
return function retryTask(...args) { | |
let innerTimes = times; | |
return new Promise(async function loop(resolve, reject) { | |
innerTimes -= 1; | |
try { | |
const res = await func.apply(this, args); | |
resolve(res); | |
} catch (err) { | |
if (innerTimes <= 0) { | |
reject(err); | |
} else { | |
setTimeout(loop.bind(this, resolve, reject), interval); | |
} | |
} | |
}); | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment