javascript - 记忆和重复的承诺
问题描述
我最近写了一个函数来重复调用一个函数(返回一个承诺),直到它成功。我的第一个想法是这样做:
//Takes a function that returns a Promise
static promiseKeepTrying(func) {
const callFunc = () => {
return func()
.catch(() => {
return this.promiseTimeout(5000)
.then(() => callFunc())
})
}
return callFunc()
}
static promiseTimeout(time) {
return new Promise(resolve => setTimeout(resolve, time, time))
}
但我担心每个新调用都会创建一个与前一个调用关联的新 Promise,从而导致内存(缓慢)被消耗。那正确吗?
所以我选择了这个实现:
//Takes a function that returns a Promise
static promiseKeepTrying(func) {
return new Promise((resolve) => {
const callFunc = () => {
func()
.then(results => {
resolve(results)
})
.catch(() => {
setTimeout(callFunc, 5000)
})
}
callFunc()
})
}
我认为这个不会为每次重试消耗内存。那正确吗?
谢谢。
解决方案
您的第二个实现确实避免了不断增加的承诺堆栈,因此将避免内存泄漏。
如果你想用 async/await 做类似的事情,你可以这样做:
async function promiseKeepTrying(func) {
let result;
let tries = 0;
while (!result) {
try {
result = await func();
return result;
} catch(e) {
tries++;
// here, optionally you can throw after, say, 20 tries.
}
}
}
推荐阅读
- javascript - 选项卡式页面内容的搜索功能
- python - 如何将概率添加到 model.predict 输出?
- android - 如何检查其他应用程序是否具有 createScreenCaptureIntent 的权限
- angular - 角度防止多余的api调用
- javascript - Javascript 传递 id - 视图到控制器 -
- ios - 在 unwind segue、dismiss 和 popViewController 之间选择以“关闭”当前视图控制器
- linux - 64 位汇编 - 打印出参数的问题
- mysql - 如何修复“ERROR 1064 (42000): You have an error in your SQL syntax;” 在mysql中
- javascript - 动态隐藏 v-text-field
- javascript - 从嵌套数组中获取对象中的值路径