我如何运行带有超时机制的异步函数
在runTaskWithTimeout()之后, 在“任务超时错误发生”后如何成功打印“任务终于”?
谢谢。
async function runTaskWithTimeout() {
try {
const timeout = setTimeout(() => {
console.log('task timeout error happen')
throw new Error('timeout error happened')
}, 2000)
console.log('task start')
await wait(3000);
/** assume this is async function*/
clearTimeout(timeout);
console.log('task complete')
} catch (error) {
console.log('catch error', error.message);
} finally {
console.log('task finally')
}
}
async function wait(ms) {
return new Promise(resolve => {
setTimeout(() => { resolve(ms);}, ms);
});
}
after runTaskWithTimeout(),
how can i print 'task finally' successfully after 'task timeout error happen'?
thanks.
async function runTaskWithTimeout() {
try {
const timeout = setTimeout(() => {
console.log('task timeout error happen')
throw new Error('timeout error happened')
}, 2000)
console.log('task start')
await wait(3000);
/** assume this is async function*/
clearTimeout(timeout);
console.log('task complete')
} catch (error) {
console.log('catch error', error.message);
} finally {
console.log('task finally')
}
}
async function wait(ms) {
return new Promise(resolve => {
setTimeout(() => { resolve(ms);}, ms);
});
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
轻松设置超时Promise.race
。这是一个函数(与您的wait
函数类似)在延迟后解决承诺,但与wait
不同的是,它拒绝承诺并包含以下错误给定的消息:然后在 runTaskWithTimeout 中,您将超时与实际工作进行竞争:
Promise.race 用您传递给它的第一个 Promise 的结果来结算它的 Promise。因此,如果实际工作在超时之前完成(或失败),那就是
Promise.race
的承诺结果。如果超时发生在实际工作完成之前,则超时是Promise.race
的结果。实时示例使用
timeout
和wait
的随机值,因此有时超时会在工作完成之前发生,有时则不会:注意:以上只是一个草图。在完整的实现中,您需要一种方法让“实际工作”知道超时已经发生,以便它可以停止正在做的任何事情。您可能想使用
AbortController
和AbortSignal
(或者如果您不执行网络代码,则在您的环境中使用等效项)。并且您可能希望 runTaskWitHTimeout 返回“实际工作”的实现值(在上面它可以通过返回来自 wait Promise.race 的值来完成)在非错误分支中)。You can readily do a timeout with
Promise.race
. Here's a function that (like yourwait
function) settles a promise after a delay, but unlikewait
, it rejects the promise with an error containing the given message:Then in
runTaskWithTimeout
, you race that timeout with the actual work:Promise.race
settles its promise with the result of the first promise you pass it to settle. So if the actual work is finished (or fails) before the timeout, that's the result of the promise fromPromise.race
. If the timeout happens before the actual work is complete, the timeout is the result fromPromise.race
.Live Example using random values for
timeout
andwait
so sometimes the timeout happens before the work is done, and other times it doesn't:Note: The above is just a sketch. In a full implementation, you'd want a way for the "real work" to know the timeout had occurred so it could stop whatever it was doing. You'd probably want to use an
AbortController
andAbortSignal
for that (or equivalents in your environment if you're not doing web code). And you'd probably wantrunTaskWitHTimeout
to return the fulfillment value of the "real work" (which it could do in the above by just returning the value fromawait Promise.race
in the non-error branch).