Skip to content

Instantly share code, notes, and snippets.

@mactanxin
Created July 6, 2020 07:58
Show Gist options
  • Save mactanxin/047e521ec1c945dd8c17e4c07bde738e to your computer and use it in GitHub Desktop.
Save mactanxin/047e521ec1c945dd8c17e4c07bde738e to your computer and use it in GitHub Desktop.
let retry = function(fn, maxTime) {
let counter = 0;
function execFunc () {
return new Promise((resolve, reject) => {
console.log(`第${counter}次请求`);
resolve(fn);
}).then(res => {
return Promise.resolve(res);
}).catch(e=>{
counter += 1;
if(counter >= maxTime) {
return Promise.reject(e)
} else {
return execFunc()
}
})
}
return execFunc()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment