ChainAsyncSequence

public struct ChainAsyncSequence<P, Q> : AsyncSequence where P : AsyncSequence, Q : AsyncSequence, P.Element == Q.Element
extension ChainAsyncSequence: AsyncIteratorProtocol

An asynchronous sequence that chains two async sequences.

The combined sequence first emits the all the values from the first sequence and then emits all values from the second.

let sequenceA = AsyncStream<Int> { continuation in
    continuation.yield(1)
    continuation.yield(2)
    continuation.yield(3)
    continuation.finish()
}

let sequenceB = AsyncStream<Int> { continuation in
    continuation.yield(4)
    continuation.yield(5)
    continuation.yield(6)
    continuation.finish()
}

let sequenceC = AsyncStream<Int> { continuation in
    continuation.yield(7)
    continuation.yield(8)
    continuation.yield(9)
    continuation.finish()
}

for await value in sequenceA <> sequenceB <> sequenceC {
    print(value)
}

// Prints:
// 1
// 2
// 3
// 4
// 5
// 6
// 7
// 8
// 9
  • The kind of elements streamed.

    Declaration

    Swift

    public typealias Element = P.Element

Initialization

  • Creates an async sequence that combines the two async sequence.

    Declaration

    Swift

    public init(
        _ p: P,
        _ q: Q
    )

    Parameters

    p

    The first async sequence.

    q

    The second async sequence.

AsyncSequence

  • Creates an async iterator that emits elements of this async sequence.

    Declaration

    Swift

    public func makeAsyncIterator() -> ChainAsyncSequence<P, Q>

    Return Value

    An instance that conforms to AsyncIteratorProtocol.

AsyncIteratorProtocol

  • next() Asynchronous

    Produces the next element in the sequence.

    Declaration

    Swift

    public mutating func next() async rethrows -> Element?

    Return Value

    The next element or nil if the end of the sequence is reached.