async Task<IEnumerable<T>> throws "is not an iterator interface type" error
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>>
?
old question, and the accepted answer is correct, however now with c#8, IAsyncEnumerable was introduced. so instead of IEnumerable you should use the IasyncEnumerable. see the docs at https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-8#asynchronous-streams