0

Is there any difference in the context of execution in these two Azure functions:

public Task<IActionResult> GetValues()
{
   return _service.GetValuesAsync()
}
public async Task<IActionResult> GetValues()
{
   return  await _service.GetValuesAsync()
}
Alexey Manoilo
  • 103
  • 1
  • 5
  • 1
    In this example no, they will both produce the same result. However one will plumb up the CIL for an `IAsynStateMachine` implementation. Also this has got to be a duplicate of 1000 other questions – TheGeneral Jan 21 '21 at 08:24
  • This is a likely candidate https://stackoverflow.com/questions/21033150/any-difference-between-await-task-run-return-and-return-task-run and this https://stackoverflow.com/questions/19098143/what-is-the-purpose-of-return-await-in-c – TheGeneral Jan 21 '21 at 08:26
  • 1
    *when nothing else is going on*, the differences are subtle, and mostly relate to exceptions; the first is more efficient (it avoids a state machine etc in the async case), but the second will have a more accurate stack-trace in some cases; if this is *application* level code (it looks like once per HTTP hit?), then the efficiency aspect is irrelevant - that mostly applies to library code like IO work, where you might perform hundreds or thousands of such operations in a loop for a single application-level call (HTTP hit, etc) - and *then* the allocation overhead might start to matter. – Marc Gravell Jan 21 '21 at 08:27
  • 1
    https://blog.stephencleary.com/2016/12/eliding-async-await.html – TheGeneral Jan 21 '21 at 08:42

0 Answers0