Explain async await. How can you join multiple async await calls together?

  • async await keywords are used for asynchronous operations
  • awaiting a process puts in a separate task and returns the reference to that task.
  • Once complete the runtime makes sure that the process starts execution back from where the await process occurs. This is called as continuation.
  • we can use Task.WhenAll() method over a list of Tasks, which will ensure all the calls within the collection are completed before flagging as complete.
var tasks = new List<Task<int>>();
for (int i = 0; i < 10; i++)
{
    tasks.Add(Task.Run<int>(() =>
    {
        Thread.Sleep(100);
        return i;
    }));
}
int[] task = await Task.WhenAll(tasks);
foreach (var i in task)
{
   Console.WriteLine(i);
}
  • async await keywords are used for asynchronous operations
  • awaiting a process puts in a separate task and returns the reference to that task.
  • Once complete the runtime makes sure that the process starts execution back from where the await process occurs. This is called as continuation.
  • we can use Task.WhenAll() method over a list of Tasks, which will ensure all the calls within the collection are completed before flagging as complete.
var tasks = new List<Task<int>>();
for (int i = 0; i < 10; i++)
{
    tasks.Add(Task.Run<int>(() =>
    {
        Thread.Sleep(100);
        return i;
    }));
}
int[] task = await Task.WhenAll(tasks);
foreach (var i in task)
{
   Console.WriteLine(i);
}
Ram
Ram

I'm a full-stack developer and a software enthusiast who likes to play around with cloud and tech stack out of curiosity. You can connect with me on Medium, Twitter or LinkedIn.

Leave a Reply

Your email address will not be published. Required fields are marked *