Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
rlziii committed Mar 30, 2023
2 parents f46698a + b108243 commit 8613f2c
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions Sources/RZExtensions/Swift/AsyncSequence+forEach.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
public extension AsyncSequence {
/// Calls the given closure on each element in the sequence in the same order
/// as a `for`-`await`-`in` or `for`-`try`-`await`-`in` loop.
///
/// This works very similar to a regular `Sequence`'s `forEach` method.
///
/// - Parameter body: A closure that takes an element of the sequence as a
/// parameter. This version must be `async` and can be a throwing or non-throwing closure.
func forEach(_ body: (Element) async throws -> Void) async rethrows {
for try await element in self {
try await body(element)
}
}

/// Calls the given closure on each element in the sequence in the same order
/// as a `for`-`await`-`in` or `for`-`try`-`await`-`in` loop.
///
/// This works very similar to a regular `Sequence`'s `forEach` method.
///
/// - Parameter body: A closure that takes an element of the sequence as a
/// parameter. This version cannot be `async` and can be a throwing or non-throwing closure.
func forEach(_ body: (Element) throws -> Void) async rethrows {
for try await element in self {
try body(element)
}
}
}

0 comments on commit 8613f2c

Please sign in to comment.