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?
log_message
does not need to be async because it's just logging the messageDone
once the message it's saved. I would not make it async just for that reason only. – Fossorial