简体   繁体   English

节点js等待在catch块抛出错误中进行的异步调用

[英]Node js await for an async call made in catch block throwing error

I'm completely new to javascript and was learning how to use await with async functions.我对 javascript 完全陌生,正在学习如何将await与异步函数一起使用。 In my case I'm trying to wait for an async function to complete which I've called from a catch block.就我而言,我正在尝试等待从 catch 块调用的异步函数完成。 It isn't working with the message The await operator can only be used with async functions .它不适用于消息The await operator can only be used with async functions

begin().then( () =>
    console.log("done")
)

async function begin() {
    await shouldFail().then( message =>
        console.log(message)
    ).catch( () => {
        await shouldFail().then( message => // The await here is not working. 
            console.log(message)
        ).catch( message =>
            console.log(message)
        )
    })
}

async function shouldFail(){
    await sleep(1000)
    return new Promise((resolve, reject) => reject('failed'))
}

I can understand this could be happening because the catch method might not be async.我可以理解这可能会发生,因为 catch 方法可能不是异步的。 I've tried to simplify my case with the above code.我试图用上面的代码简化我的案例。 What would be the best way to know when the async function called inside the catch block is completed?知道何时在 catch 块内调用的异步函数完成的最佳方法是什么? I want the begin to only return after the catch block has executed.我希望开始只在 catch 块执行后返回。

According to definition: await can be used only in async functions.根据定义:await 只能在异步函数中使用。

So your nested function has to async be as well所以你的嵌套函数也必须异步

async function begin() {
    await shouldFail().then( message =>
        console.log(message)
    ).catch( async () => { // here -----------------
        await shouldFail().then( message => 
            console.log(message)
        ).catch( message =>
            console.log(message)
        )
    })
}
begin().then( () => console.log("done") ) async function begin() { await shouldFail().then( message => console.log(message) ).catch(async () => { //**Just add a async here** await shouldFail().then( message => // This should work now console.log(message) ).catch( message => console.log(message) ) }) } async function shouldFail(){ await sleep(1000) return new Promise((resolve, reject) => reject('failed')) }

when you use async/await then you don't need to add the then and catch block.当您使用 async/await 时,您不需要添加 then 和 catch 块。

You can read more about async/await on MDN您可以在MDN上阅读有关 async/await 的更多信息

Sample code with async/await带有异步/等待的示例代码

begin().then(() =>
    console.log("done")
)
async function begin() {
    try {
        let message = await shouldFail();
        console.log(message)
    } catch (e) {
        try {
            let message = await shouldFail();
            console.log(message)
        } catch (error) {
            console.log(message)
        }
    }
}
async function shouldFail() {
    await sleep(1000)
    return new Promise((resolve, reject) => reject('failed'))
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM