ZipAsyncSequence
public struct ZipAsyncSequence<P, Q> : AsyncSequence where P : AsyncSequence, Q : AsyncSequence
extension ZipAsyncSequence: AsyncIteratorProtocol
An asynchronous sequence that applys a zip function to the two async sequences.
Use ZipAsyncSequence
to combine the latest elements from two async sequences and emit a tuple.
The async sequence waits until both provided async sequences have emitted an element, then emits both elements as a tuple.
If one sequence never emits a value or raises an error then the zipped sequence will finish.
let streamA = .init { continuation in
continuation.yield(1)
continuation.yield(2)
continuation.finish()
}
let streamB = .init { continuation in
continuation.yield(5)
continuation.yield(6)
continuation.yield(7)
continuation.finish()
}
for await value in streamA.zip(streamB) {
print(value)
}
// Prints:
// (1, 5)
// (2, 6)
-
The kind of elements streamed.
Declaration
Swift
public typealias Element = (P.Element, Q.Element)
-
Creates an async iterator that emits elements of this async sequence.
Declaration
Swift
public func makeAsyncIterator() -> ZipAsyncSequence<P, Q>
Return Value
An instance that conforms to
AsyncIteratorProtocol
.
-
next()
AsynchronousProduces the next element in the sequence.
Continues to call
next()
on it’s base iterator and iterator of it’s combined sequence.If any iterator returns
nil
, indicating the end of the sequence, this iterator returnsnil
.Declaration
Swift
public mutating func next() async rethrows -> Element?
Return Value
The next element or
nil
if the end of the sequence is reached.