Initializer

init(unfolding:onCancel:)

Constructs an asynchronous stream from a given element-producing closure, with an optional closure to handle cancellation.

Declaration 宣言

init(unfolding produce: @escaping () async -> Element?, onCancel: (@Sendable () -> Void)? = nil)

Parameters パラメータ

produce

A closure that asynchronously produces elements for the stream.

onCancel

A closure to execute when canceling the stream’s task.

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 an asynchronous stream. The created stream handles conformance to the AsyncSequence protocol automatically, including termination (either by cancellation or by returning nil from the closure to finish iteration).

The following example shows an AsyncStream created with this initializer that produces random numbers on a one-second interval. This example uses the Swift multiple trailing closure syntax, which omits the unfolding parameter label.


let stream = AsyncStream<Int> {
        await Task.sleep(1 * 1_000_000_000)
        return Int.random(in: 1...10)
    }
    onCancel: { @Sendable () in print ("Canceled.") }
)


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