How and when to use ‘async’ and ‘await’

前端 未结 21 1741
你的背包
你的背包 2020-11-21 05:07

From my understanding one of the main things that async and await do is to make code easy to write and read - but is using them equal to spawning background threads to perfo

21条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-21 05:36

    The way I understand it is also, there should be a third term added to the mix: Task.

    Async is just a qualifier you put on your method to say it's an asynchronous method.

    Task is the return of the async function. It executes asynchronously.

    You await a Task. When code execution reaches this line, control jumps out back to caller of your surrounding original function.

    If instead, you assign the return of an async function (ie Task) to a variable, when code execution reaches this line, it just continues past that line in the surrounding function while the Task executes asynchronously.

提交回复
热议问题