-
Notifications
You must be signed in to change notification settings - Fork 4.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Proposal] async support for yield keyword #1410
Comments
Your code is incorrect. Task<IEnumerable> is awaitable task (which can be awaited once) and then results can be retrieved from this task. |
Are you proposing a |
There is already a proposal to accomplish this: #261. But the result wouldn't be |
@paulomorgado Actually the problem is Well, if my problem can be solved then public static async Task<IEnumerable<TResult>> Unwrap<TSource, TResult>(this IEnumerable<Task<T>> source)
{
foreach (var i in source)
{
yield return await i;
}
} Thank you for your kind comment :-) |
@KalitaAlexey It may be difficult to define the clear concept for yielding in an async method, and it may have to take serious adjustment to compiler code generation for this issue. |
Indeed, but Also note that you can accomplish all of this today with the Microsoft Rx Framework, which supports LINQ operations over |
I am tempted to close this as a dup of #261, as that is intended to support the scenarios. |
Dup of #261 |
I would like to second the Rx-IObservable idea. I think this makes the best sense. Like this: public async IObservable GetItemsAsync()
} SOURCE: https://www.interact-sw.co.uk/iangblog/2013/11/29/async-yield-return |
I would recommend to use the AsyncEnumerator library. Example:
You can find more on GitHub page, and use it via NuGet package. |
Hi all., this requirement is triggered when I try to implement a general
SelectAsync
extension method forIEnumerable<T>
type for an async selector in current version of C#. I wish the source code to beBut the compiler does not accept the above code and says
yield
is not invalid in this context because the return type of this method is notIEnumerable<T>
type.The above code can also make a little change and thus will be another form: If we remove the
async
andawait
keyword, the return type is nowIEnumerable<Task<T>>
, and the next step for us should be convertIEnumerable<Task<T>>
intoTask<IEnumerable<T>>
, this is just the proposal decribled in #261.The async method pattern and iteration pattern are both great in C#, and I'm strongly wishing a greater combination for them.
Thank you~~
The text was updated successfully, but these errors were encountered: