Fail

public struct Fail<Element, Failure> : AsyncSequence where Failure : Error
extension Fail: AsyncIteratorProtocol

An asynchronous sequence that immediately throws an error when iterated.

Once the error has been thrown, the iterator will return nil to mark the end of the sequence.

let stream = Fail<Int, TestError>(error: TestError())

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

// Prints:
// Error!

Initialization

  • Creates an async sequence that throws an error.

    Declaration

    Swift

    public init(error: Failure)

    Parameters

    error

    The error to throw.

AsyncSequence

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

    Declaration

    Swift

    public func makeAsyncIterator() -> Fail<Element, Failure>

    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 throws -> Element?

    Return Value

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