This is something that always confuses me about async-await
. Whenever I create a function that uses await
, that function must have the modifier async
. But then a function that await
s that function must also be marked async
. It's an endless chain. Can someone help me understand how the chain ends?
Asked
Active
Viewed 71 times
1

Micro Optimizer
- 227
- 1
- 6
-
Console app, MVC, WPF, Windows Forms, or Web API? – EJoshuaS - Stand with Ukraine Nov 01 '16 at 21:16
-
call `await` when you need to get the value of `T` for a method that returns `Taks
`. So if your method returns Task – Mike_G Nov 01 '16 at 21:23, but that method doesn't use T, you can just return Task -
In case the duplicate does not answer your question, in general it ends: 1. either with a method that does not await (which means that after calling `async` method, and after the first blocking await in that method, it continues the execution) 2. or with a method which uses a different mechanism to block the execution (not `await`), e.g. `Task task = DoAsync(); task.Wait();` – Marek Fekete Nov 01 '16 at 21:27
-
1I have a related question: every method either throws, goes into an infinite loop, or it returns, and a method which returns simply transfers control to *another* method. Therefore every program either crashes or runs forever. How is it possible that any program ever ends without crashing? Funnily enough, the answer to my question and the answer to your question are the same. – Eric Lippert Nov 02 '16 at 00:34
-
@EricLippert: "every method either throws, goes into an infinite loop, or it returns". `Environment.FailFast`? :-) – Luca Cremonesi Nov 02 '16 at 15:27
-
1@LucaCremonesi: Indeed. More generally: the supposition that every method returns to another method or runs forever is false. I'm hoping that the original poster will examine the suppositions that (1) the caller of an async method must await the result; there is no such requirement, and (2) that every method returns normally; again, there is no such requirement. – Eric Lippert Nov 02 '16 at 16:08