From ac93879a674b4e079d829c95046a7ef45b053f17 Mon Sep 17 00:00:00 2001 From: ichiho <ichibon0@gmail.com> Date: Tue, 25 Jun 2024 23:55:55 +0900 Subject: [PATCH] Add waitForAll method --- Sources/AsyncAlgorithms/AsyncSquence.swift | 17 +++++++++++ .../TestAsyncSequence.swift | 29 +++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 Sources/AsyncAlgorithms/AsyncSquence.swift create mode 100644 Tests/AsyncAlgorithmsTests/TestAsyncSequence.swift diff --git a/Sources/AsyncAlgorithms/AsyncSquence.swift b/Sources/AsyncAlgorithms/AsyncSquence.swift new file mode 100644 index 00000000..23574aa6 --- /dev/null +++ b/Sources/AsyncAlgorithms/AsyncSquence.swift @@ -0,0 +1,17 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the Swift Async Algorithms open source project +// +// Copyright (c) 2022 Apple Inc. and the Swift project authors +// Licensed under Apache License v2.0 with Runtime Library Exception +// +// See https://swift.org/LICENSE.txt for license information +// +//===----------------------------------------------------------------------===// + +extension AsyncSequence where Element == Void { + /// Wait for an asynchronous sequence to complete. + public func waitForAll() async rethrows { + for try await _ in self {} + } +} diff --git a/Tests/AsyncAlgorithmsTests/TestAsyncSequence.swift b/Tests/AsyncAlgorithmsTests/TestAsyncSequence.swift new file mode 100644 index 00000000..9d84d3dd --- /dev/null +++ b/Tests/AsyncAlgorithmsTests/TestAsyncSequence.swift @@ -0,0 +1,29 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the Swift Async Algorithms open source project +// +// Copyright (c) 2022 Apple Inc. and the Swift project authors +// Licensed under Apache License v2.0 with Runtime Library Exception +// +// See https://swift.org/LICENSE.txt for license information +// +//===----------------------------------------------------------------------===// + +import XCTest +import AsyncAlgorithms + +final class TestAsyncSequence: XCTestCase { + func test_throwing_waitForAll() async throws { + let source = 1...5 + + let sequence = source.async.map { + _ = try throwOn(4, $0) + return () + } + do { + try await sequence.waitForAll() + } catch { + XCTAssertTrue(error is Failure) + } + } +}