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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With