Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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