async Task<IEnumerable<T>> throws "is not an iterator interface type" error

The following code is throwing is not an iterator interface type only when I use async await and wrap the IEnumerable with Task. If I remove async await, I can go with IEnumerable<List<T>>.

private async Task<IEnumerable<List<T>>> GetTableDataAsync<T>(CloudTable cloudTable, TableQuery<T> tableQuery)
        where T : ITableEntity, new()
    {
        TableContinuationToken contineousToken = null;
        do
        {
            var currentSegment = await GetAzureTableDateAsync(cloudTable, tableQuery, contineousToken);
            contineousToken = currentSegment.ContinuationToken;
            yield return currentSegment.Results;

        } while (contineousToken != null);

    }

Though I can consider Rx, I am not sure what is causing this issue.

Jon Skeet
people
quotationmark

Only methods declaring that they return IEnumerable<T>, IEnumerable, IEnumerator or IEnumerator<T> can be implemented with iterator blocks. That rules out all async methods.

Fundamentally it's not clear how they'd work anyway, given that IEnumerable<T> is pull-based, whereas asynchronous methods are more reactive. Also, the point of an iterator block is that callers can see intermediate results - whereas the task returned from an async method will not complete until the async method itself has completed.

You'll need to go for an alternative approach - whether that's Rx or something else. You might want to think first not about what the implementation will look like, but what the caller will do. Perhaps you actually want an IEnumerable<Task<List<T>>?

people

See more on this question at Stackoverflow