Defer.cs 1.0 KB

1234567891011121314151617181920212223242526272829
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the Apache 2.0 License.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Collections.Generic;
  5. using System.Threading.Tasks;
  6. using static System.Linq.AsyncEnumerable;
  7. namespace System.Linq
  8. {
  9. public static partial class AsyncEnumerableEx
  10. {
  11. public static IAsyncEnumerable<TSource> Defer<TSource>(Func<IAsyncEnumerable<TSource>> factory)
  12. {
  13. if (factory == null)
  14. throw new ArgumentNullException(nameof(factory));
  15. return CreateEnumerable(ct => factory().GetAsyncEnumerator(ct));
  16. }
  17. public static IAsyncEnumerable<TSource> Defer<TSource>(Func<Task<IAsyncEnumerable<TSource>>> factory)
  18. {
  19. if (factory == null)
  20. throw new ArgumentNullException(nameof(factory));
  21. return CreateEnumerable(async ct => (await factory().ConfigureAwait(false)).GetAsyncEnumerator(ct));
  22. }
  23. }
  24. }