As far as I understand it, the await keyword prevents a function from continuing until the called asynchronous function completes. Essentially, it makes the asynchronous function synchronous. So that the following holds:
await MyAsyncMethod(); // first this is called
await MyOtherAsyncMethod(); // only when the above completes, this is called
MyNonAsyncMethod(); // similarly, this is only called when the above completes
Now in most code editors, omitting the await keyword results in a warning. This is baffling to me given my understanding of what async is supposed to accomplish. For if we await every asynchronous method, are we not thereby making them all synchronous, and thus defeating the very purpose of asynchronous programming?
For example, in my specific use-case, I have a function that saves a blogpost to a database, and emails all of the blog's subscribers to notify them that a new post has been created. From the perspective of the user that creates the blogpost, it is pointless to wait for the result of the mailing. The mailing logic is a simple for loop that just sends emails out to the subscribers and returns no information worth considering. The user only cares that the blogpost has been created, and that the mailing logic has initiated, and he wants to receive this information promptly. I have therefore left out the await keyword and have allowed the process to continue in the background, while returning to the user a value that indicates that all is OK.
public async Task<IActionResult> Create(Blogpost blogpost)
{
try
{
// save to db
await CreateBlogPostAsync(blogpost);
// initiate mailing logic
NotifySubscribersAsync();
return Ok(blogpost);
}
catch
{
// return error code
return StatusCode(500);
}
}
This works perfectly for my use-case. I don't want the user to have to wait a long time for the sending process to complete before he receives an OK signal. I thought that this is precisely what asynchronous programming was meant to accomplish: allowing processes to happen in the background while the function continues. Yet, though the code compiles and runs alright for my purposes, VSCode warns me that I should be using the await keyword.
What is it I am not understanding here?
So what is the point of awaiting something. The main idea of asynchronous programming is to have a blocking task like IO run in the background, as to not block the main thread and keep your application specific.
Why does Visual Studio Code tell you to await your function call
Well, because most of the time you may want the task to run in the background, but the lines below your function call may depend on the function completing first.
That is the default assumption when your function returns a task.
If you wan't to just call your function and don't care about it's return change it's return type to void. If you do so Visual Studio Code should not bother you.