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
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.and the second line
waits for the operation on Azure to complete. See the docs regarding
WaitForCompletionAsync
: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.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).