AsyncSequence is a Swift concurrency type that provides asynchronous, sequential, iterated access to its elements.
AsyncSequence
is a Swift concurrency type that provides asynchronous, sequential, iterated access to its elements.
An AsyncSequence
resembles the Sequence
type — offering a list of values you can step through one at a time — and adds asynchronicity. An AsyncSequence
may have all, some, or none of its values available when you first use it. Instead, you use await to receive values as they become available.
As with Sequence
, you typically iterate through an AsyncSequence
with a for await-in
loop. However, because the caller must potentially wait for values, you use the await
keyword.
Note, while Apple provides a variety of built-in AsyncSequence
implementations (e.g. bytes(for:delegate:)
in URLSession
), developers can either write their own AsyncSequence
or, more often, create a AsyncStream
object to wrap a legacy process that emits values over time.