是否总是需要在等待状态下调用异步函数? [英] Does an async function always need to be called with await?

查看:204
本文介绍了是否总是需要在等待状态下调用异步函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我有一个异步函数,它将消息记录在MongoDB数据库上。

 异步函数log_message(sender,对话ID,文字){
尝试{
const msg = new Message({
发件人:发件人,
对话ID:sessionId,
文字:文字
} );
等待msg.save();
console.log( Done);
} catch(e){
console.log(记录消息时出错: + e)
}
}
触发此函数后,我立即调用 log_message将消息记录在数据库中,但我不想使用await来调用它,否则,我将等到 log_message函数返回后再处理消息会减慢消息处理速度。

 异步getReply(用户名,消息){
log_message( user,用户名, 信息);
console.log( HI);
let reply =等待this.rs.reply(用户名,消息,此);
回复;
}

不过,Jetbrains Webstorm给我这个警告缺少等待异步函数调用。
现在,我进行了一些测试,如果我不等待就调用该函数,则系统将按预期运行,消息将得到处理,并且我的日志记录函数将数据异步写入db而不中断。
如果相反,我在调用日志记录函数之前放了await关键字,则主函数中代码的执行将被挂起,直到未写入数据库为止。



有人可以告诉我使用async / await关键字的方式是否存在一些缺陷?

解决方案

它如果您的逻辑不需要 async 调用的结果,则没有必要。尽管您不需要使用文档列出了启用该警告的两个好处:


虽然通常没有必要,但它提供了两个主要好处。
第一个是,在使用try-catch包围代码时,您不会忘记添加 await。
第二个是具有明确的 await帮助V8运行时提供异步堆栈跟踪



First some context, I have an async function which logs messages on a MongoDB database.

async function log_message(sender, conversationId, text) {
    try {
        const msg = new Message({
            sender: sender,
            conversationId: conversationId,
            text: text
        });
        await msg.save();
        console.log("Done");
    } catch (e) {
        console.log("An error happened while logging the message: " + e)
    }
}

Now, I have another async function that gets triggered when I receive a message and takes care of processing it and fetching some data. As soon as this function get triggered I call "log_message" to log the message on my database, but I do not want to call it with await, otherwise, I would wait until the "log_message" function returns before processing the message slowing down the message processing.

    async getReply(username, message) {
        log_message("user", username, message);
        console.log("HI");
        let reply = await this.rs.reply(username, message, this);
        return reply;
    }

Nevertheless, Jetbrains Webstorm gives me this warning "Missing await for an async function call". Now, I did some tests and if I call the function without await the system behaves as I expect, the message gets processed and my logging function writes the data on the db asynchronously without interrupting. If instead, I put the await keyword before calling the logging function the execution of the code in the main function gets suspended until the db has not been written.

Could someone tell me whether there are some flaws in the way I intended the usage of the async/await keywords?

解决方案

It is not necessary if your logic doesn't require the result of the async call. Although not needed in your case, the documentation lists two benefits to having that warning enabled:

While this is generally not necessary, it gives two main benefits. The first one is that you won't forget to add 'await' when surrounding your code with try-catch. The second one is that having explicit 'await' helps V8 runtime to provide async stack traces

这篇关于是否总是需要在等待状态下调用异步函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆