在继续下一行代码之前等待异步递归函数完成
Wait for async recursive function to complete before proceeding to next line of code
我正在尝试等待递归函数完全完成,然后再继续下一行代码。我正在使用 PuppeteerJS 在页面上查找项目,如果该项目不存在,则在 3 秒后重新加载页面并重试。在继续之前,我需要完成此功能。下面是我要实现的简单代码示例。
(async () => {
await waitForThisToFinish() // Wait for this function no matter how long it takes
console.log("Don't log until function has completed")
})()
async function waitForThisToFinish() {
try {
await findItemOnPage() //function times out after 3 seconds
}
catch (ex) {
// If Item is not found after 3 seconds an error is thrown.
// Catch error and reload page to see if item has been loaded and look again.
waitForThisToFinish() //recursive call
}
}
目前,如果第一次尝试找不到该项目,则会抛出错误并成功开始递归。但是,代码会继续执行,不会等待函数成功完成。
有没有办法让'catch'不解析?我是否需要 return 来自 waitForThisToFinish() 函数的承诺?这将如何与递归一起工作?任何帮助将不胜感激。
我建议使用一个在成功时跳出的循环,因为这样就不会以任何方式积累资源(比如 promises),并且如果需要,您可以无限次地调用您的函数而不会产生压力资源使用。
async function waitForThisToFinish() {
while (true) {
try {
let val = await findItemOnPage()
// use break or return here to break out of the loop
return val;
} catch (ex) {
console.log("still waiting and trying again")
}
}
}
此外,您还应该进行一些额外的更改:
- 检查实际错误以确保它是可以通过重试修复的错误类型(例如超时)。如果这是一个永久性错误,您将永远重试。
- 在重试之前实施延迟以避免在服务器端造成雪崩故障并避免服务器因速率限制而阻塞。
- 在延迟中实施渐进式退避。
因为您通常不希望在出现错误时编写破坏服务器的代码(通过快速连续地一遍又一遍地重复发出相同的请求),因为这可能会导致雪崩式服务器故障,从而导致小问题发生很快就会变成一个大问题,您可能应该在再次尝试之前实施延迟,问题持续时间越长,延迟时间越长。
function delay(t) {
return new Promise(resolve => {
setTimeout(resolve, t);
});
}
async function waitForThisToFinish() {
let waitTime = 100;
while (true) {
try {
let val = await findItemOnPage()
// use break or return here to break out of the loop
return val;
} catch (ex) {
// Should also check if the actual error is one that is likely
// temporary. Otherwise, you may loop forever on a permanent error
console.log("still waiting and trying again")
// implement super simple backoff (there are much more elegant algorithms available)
await delay(waitTime);
waitTime += 300;
}
}
}
我正在尝试等待递归函数完全完成,然后再继续下一行代码。我正在使用 PuppeteerJS 在页面上查找项目,如果该项目不存在,则在 3 秒后重新加载页面并重试。在继续之前,我需要完成此功能。下面是我要实现的简单代码示例。
(async () => {
await waitForThisToFinish() // Wait for this function no matter how long it takes
console.log("Don't log until function has completed")
})()
async function waitForThisToFinish() {
try {
await findItemOnPage() //function times out after 3 seconds
}
catch (ex) {
// If Item is not found after 3 seconds an error is thrown.
// Catch error and reload page to see if item has been loaded and look again.
waitForThisToFinish() //recursive call
}
}
目前,如果第一次尝试找不到该项目,则会抛出错误并成功开始递归。但是,代码会继续执行,不会等待函数成功完成。
有没有办法让'catch'不解析?我是否需要 return 来自 waitForThisToFinish() 函数的承诺?这将如何与递归一起工作?任何帮助将不胜感激。
我建议使用一个在成功时跳出的循环,因为这样就不会以任何方式积累资源(比如 promises),并且如果需要,您可以无限次地调用您的函数而不会产生压力资源使用。
async function waitForThisToFinish() {
while (true) {
try {
let val = await findItemOnPage()
// use break or return here to break out of the loop
return val;
} catch (ex) {
console.log("still waiting and trying again")
}
}
}
此外,您还应该进行一些额外的更改:
- 检查实际错误以确保它是可以通过重试修复的错误类型(例如超时)。如果这是一个永久性错误,您将永远重试。
- 在重试之前实施延迟以避免在服务器端造成雪崩故障并避免服务器因速率限制而阻塞。
- 在延迟中实施渐进式退避。
因为您通常不希望在出现错误时编写破坏服务器的代码(通过快速连续地一遍又一遍地重复发出相同的请求),因为这可能会导致雪崩式服务器故障,从而导致小问题发生很快就会变成一个大问题,您可能应该在再次尝试之前实施延迟,问题持续时间越长,延迟时间越长。
function delay(t) {
return new Promise(resolve => {
setTimeout(resolve, t);
});
}
async function waitForThisToFinish() {
let waitTime = 100;
while (true) {
try {
let val = await findItemOnPage()
// use break or return here to break out of the loop
return val;
} catch (ex) {
// Should also check if the actual error is one that is likely
// temporary. Otherwise, you may loop forever on a permanent error
console.log("still waiting and trying again")
// implement super simple backoff (there are much more elegant algorithms available)
await delay(waitTime);
waitTime += 300;
}
}
}