Custom awaitables for dummies

前端 未结 2 406
感动是毒
感动是毒 2020-12-05 07:05

In Async/Await FAQ, Stephen Toub says:

An awaitable is any type that exposes a GetAwaiter method which returns a valid <

2条回答
  •  时光说笑
    2020-12-05 07:51

    Why would you want a custom awaiter?

    You can see the compiler's interpretation of await here. Essentially:

    var temp = e.GetAwaiter();
    if (!temp.IsCompleted)
    {
      SAVE_STATE()
      temp.OnCompleted(&cont);
      return;
    
    cont:
      RESTORE_STATE()
    }
    var i = temp.GetResult();
    

    Edit from comments: OnCompleted should schedule its argument as a continuation of the asynchronous operation.

提交回复
热议问题