skip to Main Content

I saw some people who are using Azure invoke methods like:

var res = await func();
await res.WaitForCompletionAsync();

If await is already supposed to wait until the returned task is complete, then why is there a need for the WaitForCompletionAsync call?

2

Answers


  1. There are cases in which func() initiates a long running operation on Azure, like provisioning a resource. So the first line is to start the long running operation on Azure.

    var res = await func();
    

    and the second line

    await res.WaitForCompletionAsync();
    

    waits for the operation on Azure to complete. See the docs regarding WaitForCompletionAsync:

    This method will periodically call UpdateStatusAsync till HasCompleted is true, then return the final result of the operation.

    So it is a helper function you can call so you do not need to write the polling mechanism yourself.

    A typical example is var status = await blobClient.StartCopyFromUriAsync(sourceUri, null, cToken.Token);

    As the name implies, it starts an operation, and WaitForCompletionAsync can be used to await the completion of that operation.

    Login or Signup to reply.
  2. If await is already supposed to wait until the returned task is
    complete, then why is there a need for the WaitForCompletionAsync
    call?

    Reason being the operations are asynchronous on the Azure side. When you submit a request to Azure, it accepts the request and then completes the request asynchronously. By using WaitForCompletionAsync, you are actually waiting for the operation to complete on Azure side.

    This method will periodically poll the status of the request and the method will be completed once the request is completed (either successful or failed).

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search