ExperimentalEnumerableAwaitT, TResult(IEnumerableT, FuncT, CancellationToken, TaskTResult) Method

Creates a sequence query that streams the result of each task in the source sequence as it completes asynchronously. A CancellationToken is passed for each asynchronous evaluation to abort any asynchronous operations in flight if the sequence is not fully iterated.

Definition

Namespace: MoreLinq.Experimental
Assembly: MoreLinq (in MoreLinq.dll) Version: 3.4.0+b99a6a8cc504caf2d48372fe54a2f8116c59cd0c
C#
public static IAwaitQuery<TResult> Await<T, TResult>(
	this IEnumerable<T> source,
	Func<T, CancellationToken, Task<TResult>> evaluator
)

Parameters

source  IEnumerableT
The source sequence.
evaluator  FuncT, CancellationToken, TaskTResult
A function to begin the asynchronous evaluation of each element, the second parameter of which is a CancellationToken that can be used to abort asynchronous operations.

Type Parameters

T
The type of the source elements.
TResult
The type of the result elements.

Return Value

IAwaitQueryTResult
A sequence query that stream its results as they are evaluated asynchronously.

Usage Note

In Visual Basic and C#, you can call this method as an instance method on any object of type IEnumerableT. When you use instance method syntax to call this method, omit the first parameter. For more information, see Extension Methods (Visual Basic) or Extension Methods (C# Programming Guide).

Remarks

This method uses deferred execution semantics. The results are yielded as each asynchronous evaluation completes and, by default, not guaranteed to be based on the source sequence order. If order is important, compose further with AsOrderedT(IAwaitQueryT).

This method starts a new task where the asynchronous evaluations take place and awaited. If the resulting sequence is partially consumed then there's a good chance that some projection work will be wasted and a cooperative effort is done that depends on the evaluator function (via a CancellationToken as its second argument) to cancel those in flight.

The evaluator function should be designed to be thread-agnostic.

The task returned by evaluator should be started when the function is called (and not just a mere projection) otherwise changing concurrency options via AsSequentialT(IAwaitQueryT), MaxConcurrencyT(IAwaitQueryT, Int32) or UnboundedConcurrencyT(IAwaitQueryT) will only change how many tasks are awaited at any given moment, not how many will be kept in flight.

See Also