A closure that asynchronously produces elements for the stream.
Initializer
init(unfolding:)
Constructs an asynchronous throwing stream from a given element-producing closure.
Availability
- iOS 13.0+
- iPadOS 13.0+
- macOS 10.15+
- Mac Catalyst 15.0+
- tvOS 13.0+
- watchOS 6.0+
- Xcode 13.0+
Technology
- Swift Standard Library Swift標準ライブラリ
Declaration 宣言
init(unfolding produce: @escaping () async throws -> Element?) where Failure == Error
Parameters パラメータ
produce
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 Async
protocol automatically. To terminate the stream with an error, throw the error from your closure.
The following example shows an Async
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 My
.