-
Notifications
You must be signed in to change notification settings - Fork 0
Async Operations
Ahmad Al-freihat edited this page Jan 1, 2026
·
1 revision
All functional operations have async equivalents for asynchronous scenarios.
Transform elements asynchronously:
NonEmptyList<int> numbers = new(1, 2, 3);
NonEmptyList<string> results = await numbers.MapAsync(async x =>
{
await Task.Delay(100);
return x.ToString();
});
// Result: ["1", "2", "3"]Execute transformations in parallel:
NonEmptyList<int> numbers = new(1, 2, 3);
NonEmptyList<int> parallelResults = await numbers.MapParallelAsync(async x =>
{
await Task.Delay(100);
return x * 2;
});
// Result: [2, 4, 6] - processed in parallelLimit concurrent operations:
NonEmptyList<int> numbers = new(1, 2, 3, 4, 5, 6, 7, 8);
NonEmptyList<int> throttled = await numbers.MapParallelAsync(
async x =>
{
await Task.Delay(100);
return x * 2;
},
maxDegreeOfParallelism: 2
);
// Maximum 2 concurrent operationsFilter elements asynchronously:
NonEmptyList<int> numbers = new(1, 2, 3, 4, 5);
NonEmptyList<int>? evenNumbers = await numbers.FilterAsync(async x =>
{
await Task.Delay(10);
return x % 2 == 0;
});
// Result: [2, 4] or null if all filtered outAggregate elements asynchronously:
NonEmptyList<int> numbers = new(1, 2, 3, 4);
int sum = await numbers.FoldAsync(0, async (acc, x) =>
{
await Task.Delay(10);
return acc + x;
});
// Result: 10Check conditions asynchronously:
NonEmptyList<int> numbers = new(1, 2, 3, 4, 5);
// Check if all elements satisfy a condition
bool allPositive = await numbers.AllAsync(async x =>
{
await Task.Delay(1);
return x > 0;
});
// Result: true
// Check if any element satisfies a condition
bool hasEven = await numbers.AnyAsync(async x =>
{
await Task.Delay(1);
return x % 2 == 0;
});
// Result: trueFind first matching element asynchronously:
NonEmptyList<int> numbers = new(1, 2, 3, 4, 5);
int? firstEven = await numbers.FirstOrDefaultAsync(async x =>
{
await Task.Delay(10);
return x % 2 == 0;
});
// Result: 2Convert to IAsyncEnumerable for async iteration:
NonEmptyList<int> numbers = new(1, 2, 3);
await foreach (int item in numbers.ToAsyncEnumerable())
{
Console.WriteLine(item);
await Task.Delay(100);
}
// Output: 1, 2, 3 (with delays)NonEmptyList<int> userIds = new(1, 2, 3);
NonEmptyList<User> users = await userIds.MapParallelAsync(
async id => await httpClient.GetFromJsonAsync<User>($"/api/users/{id}"),
maxDegreeOfParallelism: 5
);NonEmptyList<Order> orders = GetOrders();
bool allValid = await orders.AllAsync(async order =>
await orderValidator.ValidateAsync(order)
);NonEmptyList<Document> documents = GetDocuments();
await documents.ForEachAsync(async doc =>
{
await ProcessDocumentAsync(doc);
await SaveResultAsync(doc);
});- Learn about ImmutableNonEmptyList for thread-safe scenarios
- See Thread Safety for concurrency considerations