Re:Ask HN: Why can't await be used from non-async functions?
by darklion on Monday February 28, 2022 @01:36PM
(#30503283)
Unfortunately, there are problems with both of those approaches: https://github.com/davidfowl/AspNetCoreDiagnosticScenarios/b...
I don't know if it extends to other language implementations, but C# does not seem to have a reliable sync-over-async story.
Re:Ask HN: Why can't await be used from non-async functions?
by tragictrash on Monday February 28, 2022 @02:27PM
(#30503908)
Seems pretty standard to me. Every platform has problems just like this, nothing new there afaik.
Re:Ask HN: Why can't await be used from non-async functions?
by Merad on Monday February 28, 2022 @01:44PM
(#30503401)
Note that it's recommend that you avoid using these because they carry a risk of deadlock. Unless something has changed recently .Net doesn't have a 100% safe way to synchronously call an async method from a non-async method.
Re:Ask HN: Why can't await be used from non-async functions?
by klodolph on Monday February 28, 2022 @01:53PM
(#30503498)
I mean, depends on what you mean by "100% safe". You can synchronously wait from a thread that's not in the thread pool executing the tasks. Obviously that's not sufficient for safety, but it should be illustrative that safety is possible. There are also some issues with using .Wait or .Result but those can be solved.
Re:Ask HN: Why can't await be used from non-async functions?
by endorphine on Monday February 28, 2022 @04:00PM
(#30505066)
Can you elaborate on why there's a risk of a deadlock?
Re:Ask HN: Why can't await be used from non-async functions?
by Merad on Monday February 28, 2022 @10:59PM
(#30509317)
The very short version is that by synchronously blocking on the async method your thread may end up holding a resource (thread context info or the calling thread itself) that the async method needs in order to run its continuations. Here are a couple of more detailed explanations:
https://blog.stephencleary.com/2012/07/dont-block-on-async-c...
https://devblogs.microsoft.com/pfxteam/await-and-ui-and-dead...
Re:Ask HN: Why can't await be used from non-async functions?
by endorphine on Tuesday March 01, 2022 @02:05AM
(#30510629)
Now that's pretty much the answer I was looking for, I believe. This makes things much clearer!
In C#, you can't use the "await" keyword, but you can use the result of a Task<T> in a non-async function with ".Result" or ".GetAwaiter().GetResult()". https://stackoverflow.com/a/47648318/5107208