0

Here's the method:

async (a) =>
{
await DoSomethingForTheFirstTimeAsync();
await DoSomethingForTheSecondTimeAsync();
await DoSomethingForTheLastTimeAsync();
}

The question is: "Will we get back to the method after the last call finishes it's work?" I fail to see any scenario where it should be needed.

  • If you don't need to get back to the previous context, then don't use await – Matias Cicero Sep 02 '16 at 13:14
  • 1
    My understanding is that it will still return and then exit the method. Why are you asking? It should make no difference to any app you are building. Also take a look at this post around the use of void async methods http://stackoverflow.com/questions/12144077/async-await-when-to-return-a-task-vs-void – Murray Foxcroft Sep 02 '16 at 13:14
  • @MurrayFoxcroft the existence of the method keeps local roots alive. I don't really have a problem with it. Asked mostly because I'm curious and couldn't find an answer if there are some specific optimizations or not. – Sergey.quixoticaxis.Ivanov Sep 02 '16 at 13:51
  • @MatiasCicero strange remark. I may want to get back to the flow, but not the context. – Sergey.quixoticaxis.Ivanov Sep 02 '16 at 13:51
  • @Sergey.quixoticaxis.Ivanov Getting back to the flow is getting back to the previous context – Matias Cicero Sep 02 '16 at 14:00
  • Take a look here for a fire and forget pattern, it seems this may be what you really need: http://stackoverflow.com/questions/22864367/fire-and-forget-approach – Murray Foxcroft Sep 02 '16 at 14:03
  • @Murray thanks, I know the pattern though^^ As I said it's more of curiosity then an actual problem. I've cleaned the roots manually. My real await actions are similar and should be awaited. it's just the fact that I don't really care for the last one, so I was curious whether or not it will hold some memory until done. – Sergey.quixoticaxis.Ivanov Sep 02 '16 at 14:21
  • @Sergey.quixoticaxis.Ivanov, you are awaiting the last one, so yes, the flow will wait for the last one to end, but not in a blocking manner. If you really just want to fire and forget, call your last method without the await. – Matias Cicero Sep 02 '16 at 14:24
  • @MatiasCicero then it will execute synchronously and I'll be waiting for it to finish. maybe I don't understand what you mean. – Sergey.quixoticaxis.Ivanov Sep 02 '16 at 15:40
  • @MatiasCicero also the same flow doesn't necessarily mean the same synchronization context. – Sergey.quixoticaxis.Ivanov Sep 03 '16 at 14:33
  • @Sergey.quixoticaxis.Ivanov if you remove the await on that method then it won't execute synchronously, please read the documentation. – Matias Cicero Sep 03 '16 at 15:37
  • @MatiasCicero ah, I misunderstood you, you mean suppress the warnings and call it without await. I thought about tasks, which would make the call synchronous, if I want to get exceptions back. Yeap, that would do the trick. Now I'm gonna test what happens with exception handling. – Sergey.quixoticaxis.Ivanov Sep 03 '16 at 17:17

1 Answers1

2

One of the scenarios where it is needed is when the last method throws an exception. Await will throw in the context of the calling method.

You may set a breakpoint on the closing brace in the method - thus, the last call returns.

dvorn
  • 3,107
  • 1
  • 13
  • 12
  • Or it will throw in some other context, if I'm not forcing the usage of persistent context. I thought about it but it does not really matter whether the exception is thrown inside or already outside of the method. – Sergey.quixoticaxis.Ivanov Sep 02 '16 at 13:50