(For simplicity, let's assume I'm talking about a command-line app that doesn't have any UI running in a separate thread.) So I've been attempting to wrap my head around async/await because there's an asynchronous function whose results I'd like to use. The issue, as I understand it, is that you can't get the results of an asynchronous function in a synchronous context. If you try to use await inside a non-async function, you get an error. You can remove that error by wrapping the function call in a Task, but getting the results from a Task, again, requires that you be in an asynchronous context.

So what is the solution? Is it basically necessary for your top-level functions to be asynchronous functions, because that's the only way for them to access the results of lower-level asynchronous functions? In other words, if you want any kind of asynchrony in your program, it needs to start at the top level? Does that mean your main function has to be asynchronous, or is that even possible?

EDIT: I'm thinking this through a bit, and I believe what I said above isn't quite right. Your main function does not need to be asynchronous. But either it or something below it needs to start up a task, and all the asynchronous function calls and processing of their results needs to happen inside that task. So if the asynchronous function calls are producing data that's important to the functioning of your program, then likely the great majority of your program will need to be running inside a task. Is that a more correct understanding?

I appreciate the help with understanding this.

Top-level concurrency is possible.

2 Likes