What does await do in this function?

后端 未结 3 1878
谎友^
谎友^ 2021-01-19 03:49

I thought I understand the async-await pattern in C# but today I\'ve found out I really do not.

In a simple code snippet like this. I have

3条回答
  •  旧巷少年郎
    2021-01-19 04:02

    It awaits the completion of the HTTP request. The code resumes (for iteration...) only after every single request is complete.

    Your 2nd version works precisely because it doesn't await for each task to complete before initiating the following tasks, and only waits for all the tasks to complete after all have been started.

    What async-await is useful for is allowing the calling function to continue doing other things while the asynchronous function is awaiting, as opposed to synchronous ("normal") functions that block the calling function until completion.

提交回复
热议问题