Hi @Shervan360 , Welcome to Microsoft Q&A,
When the await
keyword is used in an asynchronous method, it tells the program to wait for the asynchronous operation to complete before continuing with the code below. In the two examples you provided, we'll see different ways of using it.
Example 1: Using yield
and IAsyncEnumerable
, await
before foreach
In this example, we use yield
and IAsyncEnumerable
to process a series of URLs asynchronously. The await
keyword is used before the foreach
loop. Proceed as follows:
-
MainMethodForIAsyncEnumerableAndYield
is the entry point. It initializes an empty list namedoutput
and callsMyAsyncMethods.GetPageLengthsWithIAsyncEnumerableAndYield
, passing in a set of URLs: "google.com", "microsoft.com", and "amazon.com". - In
GetPageLengthsWithIAsyncEnumerableAndYield
, create anHttpClient
instance and use aforeach
loop to traverse the provided URL. - For each URL, it uses
await client.GetAsync($"http://{url}")
to perform an asynchronous HTTP request. Theawait
keyword indicates that the method will pause until the HTTP request is completed, making your code non-blocking. - While the HTTP request is in progress, control is returned to the calling code, allowing other operations to execute in parallel.
- When the HTTP request is completed, the code following
await
continues to execute. It records that the request has completed and then returns the length of the HTTP content header asynchronously usingyield return
. -
MainMethodForIAsyncEnumerableAndYield
waits for the results ofIAsyncEnumerable<long?>
and usesawait foreach (long? len in list)
to iterate these results. This loop also allows other operations to be performed in parallel while waiting for each result. - Inside the loop, it adds the page length to the
output
list. - Once all iterations are complete, the
MainMethodForIAsyncEnumerableAndYield
method returns anoutput
list containing the recorded message and page length. - In the
Main
method, you wait forMainMethodForIAsyncEnumerableAndYield()
and print the result.
Example 2: No yield
and IAsyncEnumerable
, await
inside foreach
This example also handles a sequence of URLs, but does not use yield
or IAsyncEnumerable
. Instead, it uses await
inside a foreach
loop:
-
MainMethod
is the entry point. It initializes an empty list namedoutput
and callsMyAsyncMethods.GetPageLengths
, passing in a set of URLs: "apress.com", "microsoft.com", and "amazon.com". - In
GetPageLengths
, create anHttpClient
instance and use aforeach
loop to iterate over the provided URL. - For each URL, it uses
await client.GetAsync($"http://{url}")
to perform an asynchronous HTTP request. Theawait
keyword indicates that the method will pause until the HTTP request is completed, making your code non-blocking. - But unlike the first example, there is no
yield return
in this method. Instead, it adds the page length directly to the results list and logs the request completion. - After the loop completes, it returns the
results
list. - In
MainMethod
, you useforeach
to iterate over theIEnumerable<long?>
returned byMyAsyncMethods.GetPageLengths
. - Inside the loop, it adds the page length to the
output
list. - Once all iterations are complete, the
MainMethod
method returns anoutput
list containing the recorded message and page length. - In the
Main
method, you wait forMainMethod()
and print the result.
In short, both examples use await to perform asynchronous HTTP requests. The main difference is that the first example uses yield
and IAsyncEnumerable
, allowing for more efficient streaming of results, while the second example collects all results into a list before processing them. Which method you choose to use depends on your specific needs and how you want to handle the results of asynchronous operations.
Best Regards,
Jiale
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.