init(unfolding:onCancel:)
Constructs an asynchronous stream from a given element-producing closure, with an optional closure to handle cancellation.
Declaration
@preconcurrency init(unfolding produce: @escaping @Sendable () 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 asynchronous 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)
}