Tuan Nguyen
Tuan Nguyen

Reputation: 305

should I use await on return?

I have an MVC web application, the controller action calls a code in a business layer, which in turn calls an API:

public async Task<ActionResult> TestAction(){
   Info something = await businessLayer.GetInfoAsync();
   return View(something);
}

in the business layer I have GetInfoAsync defined as follows:

public async Task<Info> GetInfoAsync(){
   return await myLib.GetAsync<Info>();
}

in my library I have GetAsync defined as follows:

public async Task<T> GetAsync(){
   //do something
   var data = await aService.Get<T>();
   return data;
}

As you can see above GetInfoAsync method just makes a call to myLib.GetAsync and returns whatever value myLib.GetAsync returns. Could I just get rid of async/await for GetInfoAsync, and define it as follows :

public Task<Info> GetInfoAsync(){
   return myLib.GetAsync<Info>();
}

Thanks !

Upvotes: 0

Views: 185

Answers (1)

Stephen Cleary
Stephen Cleary

Reputation: 457037

Yes, you can (and should) get rid of async/await in the middle layer.

If your async method is just awaiting a single task (and that's all it does), then the async just adds overhead.

Upvotes: 1

Related Questions