Skip to content

Instantly share code, notes, and snippets.

@think2011
Created January 16, 2018 14:50
Show Gist options
  • Save think2011/8f8a7a5f3279c8d6bbd02de3aa71eb45 to your computer and use it in GitHub Desktop.
Save think2011/8f8a7a5f3279c8d6bbd02de3aa71eb45 to your computer and use it in GitHub Desktop.
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