-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/main'
- Loading branch information
Showing
1 changed file
with
27 additions
and
0 deletions.
There are no files selected for viewing
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
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) | ||
} | ||
} | ||
} |