Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have two following methods

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

//overload with None token 
public /*async*/ Task<bool> DoSomething()
{
    return /*await*/ DoSomething(CancellationToken.None);
}

Should second method be marked with async/await keywords or not?

like image 856
hazzik Avatar asked Mar 16 '13 00:03

hazzik


1 Answers

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.

like image 94
Reed Copsey Avatar answered Sep 18 '22 14:09

Reed Copsey