Does anyone know what a AsyncPageable is?

Viewed 4198

I'm trying to do a get all from an Azure service and it returns an AsyncPageable. According to the doc it says

A collection of values that may take multiple service requests to iterate over.

Does that mean that it is equal to doing the request for a single item multiple times with a loop?

1 Answers

If a service call returns multiple values in pages it would return Pageable<T>/AsyncPageable<T> as a result. Check out Consuming Service Methods Returning AsyncPageable.

To get more clarity, have a look at below: This shows control over receiving pages of values from the service use AsyncPageable<T>.AsPages method:

// call a service method, which returns AsyncPageable<T>
AsyncPageable<SecretProperties> response = client.GetPropertiesOfSecretsAsync();

await foreach (Page<SecretProperties> page in response.AsPages())
{
    // enumerate through page items
    foreach (SecretProperties secretProperties in page.Values)
    {
        Console.WriteLine(secretProperties.Name);
    }

    // get continuation token that can be used in AsPages call to resume enumeration
    Console.WriteLine(page.ContinuationToken);
}

If your project doesn't have C# 8.0 enabled you can still iterate over AsyncPageable using a while loop:

// call a service method, which returns AsyncPageable<T>
AsyncPageable<SecretProperties> response = client.GetPropertiesOfSecretsAsync();

IAsyncEnumerator<SecretProperties> enumerator = response.GetAsyncEnumerator();
try
{
    while (await enumerator.MoveNextAsync())
    {
        SecretProperties secretProperties = enumerator.Current;
        Console.WriteLine(secretProperties.Name);
    }
}
finally
{
    await enumerator.DisposeAsync();
}

Check out Azure.Core Response samples to understand more about this.

To change page size, you can use pageSizeHint parameter to AsPages method.

Related