Initializer

init(unfolding:)

Constructs an asynchronous throwing stream from a given element-producing closure.

Declaration 宣言

init(unfolding produce: @escaping () async throws -> Element?) where Failure == Error

Parameters パラメータ

produce

A closure that asynchronously produces elements for the stream.

Discussion 解説

Use this convenience initializer when you have an asychronous function that can produce elements for the stream, and don’t want to invoke a continuation manually. This initializer “unfolds” your closure into a full-blown asynchronous stream. The created stream handles adherence to the AsyncSequence protocol automatically. To terminate the stream with an error, throw the error from your closure.

The following example shows an AsyncThrowingStream created with this initializer that produces random numbers on a one-second interval. If the random number is divisble by 5 with no remainder, the stream throws a MyRandomNumberError.


let stream = AsyncThrowingStream<Int, Error> {
        await Task.sleep(1 * 1_000_000_000)
        let random = Int.random(in: 1...10)
        if (random % 5 == 0) {
            throw MyRandomNumberError()
        }
        return random
    }


// Call point:
do {
    for try await random in stream {
        print ("\(random)")
    }
} catch {
    print ("\(error)")
}