Forked from DCCoder90/AsyncEnumerableExtensions.cs
Created
February 25, 2021 16:38
-
-
Save skynode/c23a07339328f9a8a9cbd0c8bd4cebec to your computer and use it in GitHub Desktop.
Convert IEnumerable to IAsyncEnumerable
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class AsyncEnumerableExtensions | |
{ | |
public static IAsyncEnumerable<TResult> SelectAsync<T, TResult>(this IEnumerable<T> enumerable, | |
Func<T, Task<TResult>> selector) | |
{ | |
return AsyncEnumerable.CreateEnumerable(() => | |
{ | |
var enumerator = enumerable.GetEnumerator(); | |
var current = default(TResult); | |
return AsyncEnumerable.CreateEnumerator(async c => | |
{ | |
var moveNext = enumerator.MoveNext(); | |
current = moveNext | |
? await selector(enumerator.Current).ConfigureAwait(false) | |
: default(TResult); | |
return moveNext; | |
}, | |
() => current, | |
() => enumerator.Dispose()); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment