Blog Post

Multiple awaits vs waitAll

Wednesday, November 1, 2017 1:27 AM

While reviewing a code I have come across code that has multiple awaits after each other. I have found similar question in stack overflow.

 

Explanation

 

The first option will not execute the two operations concurrently. It will execute the first and await its comnpletion, and only then the second.

The second option will execute both concurrently but will wait for them synchronously (i.e. while blocking a thread).

You shouldn't use both options since the first completes slower than the second and the second blocks a thread without need.

You should wait for both operations asynchronously with Task.WhenAll