Async all the way down?

前端 未结 2 2048
星月不相逢
星月不相逢 2020-12-16 13:29

Trying to understand the new async/await pattern, I have one question which I can\'t find an answer to, namely if I should decorate my methods with async, if I intend to cal

2条回答
  •  温柔的废话
    2020-12-16 14:16

    Both versions work effectively the same, the only difference is that when you use await here, you get some performance penalty (because the state machine must be set up and a continuation will most likely be used).

    So, it comes down to a tradeoff: Do you want your methods to be somewhat more efficient at the cost of being slightly less readable? Or are you willing to sacrifice performance for readability?

    Usually, I would advise you to go for readability first and only focus on performance if profiling tells you it's worth it. But in this case, I think the increase in readability is small, so I would probably not use await.

    Also note that your class C still doesn't go far enough: foo1() also doesn't need await.

提交回复
热议问题