当前位置:网站首页>实现一个支持请求失败后重试的JS方法
实现一个支持请求失败后重试的JS方法
2022-08-09 14:54:00 【好帅的爸爸()】
假设存在一个函数,返回promise对象。它可能会失败。
// 伪代码
function asyncFunc(){
return new Promise((resolve,reject)=>{
axios.get('http://sdadasdadadasdas.com').then((res)=>{
resolve(res)
}).catch((err)=>{
reject(err)
})
})
}包装上面的函数支持重试。
// 支持重试的函数
function reTry(asyncFunc,times) { //times是重试次数
return new Promise(async (resolve, reject) => {
function reTryFunc(times) {
console.log(`第${times}次重试`)
asyncFunc().then((res) => {
resolve(res)
}).catch((err) => {
if (times > 0) {
console.log(`----第${times}次重试`)
setTimeout(() => {
reTryFunc(times - 1)
})
} else {
reject(err)
}
})
}
reTryFunc(times)
})
}边栏推荐
- 复数与复数域
- Common compilation problems
- 简单记录下offsetof和container_of
- VS2010: devenv.sln solution save dialog appears
- Talking about quantitative trading and programmatic trading
- 【小白必看】初始C语言(下)
- Seize the opportunity of quantitative trading fund products, and quantitative investment has room for development?
- 跨平台桌面应用 Electron 尝试(VS2019)
- SNR 信噪比
- 用户如何正确去认识程序化交易?
猜你喜欢
随机推荐
C language operator precedence
如何让你的量化交易系统具有概率优势,具有正向收益预期呢?
数据库多表链接查询的方式
[Mysql]--Transaction, transaction isolation level, dirty read, non-repeatable read, phantom read analysis
正则化原理的简单分析(L1/L2正则化)
C#轻量级ORM使用 Dapper+Contrib
【小白必看】初始C语言(下)
cropperjs裁剪上传头像使用方法
stream去重相同属性对象
How to List < Map> grouping numerical merge sort
What drives the development of quantitative trading interfaces?
redis从入门到精通
NetCore 5.0连接MySql
注释,标识符,数据类型
Seize the opportunity of quantitative trading fund products, and quantitative investment has room for development?
二叉排序树的左旋与右旋
Simple analysis of regularization principle (L1 / L2 regularization)
常微分方程的幂级数解法
贝塞尔函数
Sort method (Hill, Quick, Heap)







