Should method that get Task and passes it away await it?

前端 未结 2 2027
渐次进展
渐次进展 2021-01-17 17:54

I have two following methods

public async Task DoSomething(CancellationToken.token) 
{
    //do something async
}

//overload with None token 
pu         


        
2条回答
  •  时光取名叫无心
    2021-01-17 18:33

    It doesn't need to - if you use await/async in the second method, you'll be adding extra overhead which accomplishes nothing, in this case.

    The async work inside of DoSomething(CancellationToken) will already provide the proper asynchronous handling and marshaling back to the existing context for you.

    At the end of the day, async and await are really just language features which make it easy to create and compose a Task. If you already have a perfectly good Task to return, there is no need to use extra language support to unwrap and rewrap it into a new Task.

提交回复
热议问题