ThrowingPassthroughAsyncSequence

public struct ThrowingPassthroughAsyncSequence<Element> : AsyncSequence

A async sequence that broadcasts elements.

let sequence = ThrowingPassthroughAsyncSequence<Int>()
sequence.yield(0)
sequence.yield(1)
sequence.yield(2)
sequence.finish(throwing: TestError())

do {
    for try await value in sequence {
      print(value)
    }
} catch {
    print("Error!")
}

// Prints:
// 0
// 1
// 2
// Error!

Initialization

  • Creates an async sequence that broadcasts elements.

    Declaration

    Swift

    public init()

AsyncSequence

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

    Declaration

    Swift

    public func makeAsyncIterator() -> AsyncThrowingStream<Element, Error>.Iterator

    Return Value

    An instance that conforms to AsyncIteratorProtocol.

API

  • Yield a new element to the sequence.

    Yielding a new element will update this async sequence’s element property along with emitting it through the sequence.

    Declaration

    Swift

    public func yield(_ element: Element)

    Parameters

    element

    The element to yield.

  • Mark the sequence as finished by having it’s iterator emit nil.

    Once finished, any calls to yield will result in no change.

    Declaration

    Swift

    public func finish()
  • Mark the sequence as finished by having it’s iterator throw the provided error.

    Once finished, any calls to yield will result in no change.

    Declaration

    Swift

    public func finish(throwing error: Error)

    Parameters

    error

    The error to throw.

  • Emit one last element beford marking the sequence as finished by having it’s iterator emit nil.

    Once finished, any calls to yield will result in no change.

    Declaration

    Swift

    public func finish(with element: Element)

    Parameters

    element

    The element to emit.