Best way to convert callback-based async method to awaitable task

前端 未结 2 2072
刺人心
刺人心 2020-11-28 20:57

What would be the best way to convert/wrap a \"classic\" asynchronous method that uses a callback to something that returns a (awaitable) Task?

For example, given th

2条回答
  •  执笔经年
    2020-11-28 21:27

    Your assumed implementation is perfectly fine for this assuming the callback only ever handles successful situations. What currently happens if an exception happens within the async underpinnings of the GetStringFromUrl implementation? There's no real way for them to propagate that to the Action callback... do they just swallow it and return you null or something?

    The only thing I would recommend is using following the new convention of naming such async methods with the XXXAsync suffix.

提交回复
热议问题