Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Async await how to use return values

Tags:

I have a windows service that I have inherited from another developer, it runs very slow and has numerous slow call to the eBay API. I wish to speed it up without too much refactoring.

I've just started to look at using c# async/await to try to get some of these slow call to run async. Here's what i'm trying to achieve:

I have a 1 very busy method that makes lots of calls as below:

getProducts getCategories getVehicles getImages 

My thoughts were that I could simply change the methods to async and add Task<T> to the return type as below:

public async Task<String> ProcessAdditionalProductDetialsAsync(ItemType oItem) {     String additionalProductDetails = string.Empty;      if (oItem.ItemSpecifics.Count > 0)     {         foreach (NameValueListType nvl in oItem.ItemSpecifics)         {                               if (nvl.Value.Count > 0)             {                 foreach (string s in nvl.Value)                 {                     additionalProductDetails += "<li><strong>" + nvl.Name + ":</strong>&nbsp;" + s + "</li>";                 }             }         }     }     return additionalProductDetails; } 

Then call them with await:

Task<String> additionalProductDetials = ebayPartNumbers.ProcessAdditionalProductDetialsAsync(item); Task<PartNumberCollection> partNumberCollection = ebayPartNumbers.ProcessPartNumbersAsync(item);    await Task.WhenAll(partNumberCollection, additionalProductDetials); 

How do I get hold of the returned types so I can use them? I have tried just using partNumberCollection but it only has the await properties available.

like image 751
Alex Stephens Avatar asked Oct 22 '14 14:10

Alex Stephens


People also ask

How can async await return values from async functions?

To return values from async functions using async-await from function with JavaScript, we return the resolve value of the promise. const getData = async () => { return await axios.

Can we return value from async function?

Async functions always return a promise. If the return value of an async function is not explicitly a promise, it will be implicitly wrapped in a promise. Note: Even though the return value of an async function behaves as if it's wrapped in a Promise.resolve , they are not equivalent.

Can we use return with await?

Using return await inside an async function keeps the current function in the call stack until the Promise that is being awaited has resolved, at the cost of an extra microtask before resolving the outer Promise.

Does await return a promise or a value?

Inside an async function, you can use the await keyword before a call to a function that returns a promise. This makes the code wait at that point until the promise is settled, at which point the fulfilled value of the promise is treated as a return value, or the rejected value is thrown.


1 Answers

Use Result property on Task class:

await Task.WhenAll(partNumberCollection, additionalProductDetials);  var partNumberCollectionResult = partNumberCollection.Result; var additionalProductDetialsResult = additionalProductDetials.Result; 
like image 95
Ufuk Hacıoğulları Avatar answered Oct 12 '22 11:10

Ufuk Hacıoğulları