<!--
{
  "availability" : [
    "iOS: 13.0.0 -",
    "iPadOS: 13.0.0 -",
    "macCatalyst: 13.0.0 -",
    "macOS: 10.15.0 -",
    "tvOS: 13.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 6.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Swift",
  "identifier" : "/documentation/Swift/AsyncSequence",
  "metadataVersion" : "0.1.0",
  "role" : "Protocol",
  "symbol" : {
    "kind" : "Protocol",
    "modules" : [
      "Swift"
    ],
    "preciseIdentifier" : "s:Sci"
  },
  "title" : "AsyncSequence"
}
-->

# AsyncSequence

A type that provides asynchronous, sequential, iterated access to its
elements.

```
protocol AsyncSequence<Element, Failure>
```

## Overview

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. The following example shows how to iterate
over `Counter`, a custom `AsyncSequence` that produces `Int` values from
`1` up to a `howHigh` value:

```
for await number in Counter(howHigh: 10) {
    print(number, terminator: " ")
}
// Prints "1 2 3 4 5 6 7 8 9 10 "
```

An `AsyncSequence` doesn’t generate or contain the values; it just defines
how you access them. Along with defining the type of values as an associated
type called `Element`, the `AsyncSequence` defines a `makeAsyncIterator()`
method. This returns an instance of type `AsyncIterator`. Like the standard
`IteratorProtocol`, the `AsyncIteratorProtocol` defines a single `next()`
method to produce elements. The difference is that the `AsyncIterator`
defines its `next()` method as `async`, which requires a caller to wait for
the next value with the `await` keyword.

`AsyncSequence` also defines methods for processing the elements you
receive, modeled on the operations provided by the basic `Sequence` in the
standard library. There are two categories of methods: those that return a
single value, and those that return another `AsyncSequence`.

Single-value methods eliminate the need for a `for await`-`in` loop, and instead
let you make a single `await` call. For example, the `contains(_:)` method
returns a Boolean value that indicates if a given value exists in the
`AsyncSequence`. Given the `Counter` sequence from the previous example,
you can test for the existence of a sequence member with a one-line call:

```
let found = await Counter(howHigh: 10).contains(5) // true
```

Methods that return another `AsyncSequence` return a type specific to the
method’s semantics. For example, the `.map(_:)` method returns a
`AsyncMapSequence` (or a `AsyncThrowingMapSequence`, if the closure you
provide to the `map(_:)` method can throw an error). These returned
sequences don’t eagerly await the next member of the sequence, which allows
the caller to decide when to start work. Typically, you’ll iterate over
these sequences with `for await`-`in`, like the base `AsyncSequence` you started
with. In the following example, the `map(_:)` method transforms each `Int`
received from a `Counter` sequence into a `String`:

```
let stream = Counter(howHigh: 10)
    .map { $0 % 2 == 0 ? "Even" : "Odd" }
for await s in stream {
    print(s, terminator: " ")
}
// Prints "Odd Even Odd Even Odd Even Odd Even Odd Even "
```

## Topics

### Creating an Iterator

[`makeAsyncIterator()`](/documentation/Swift/AsyncSequence/makeAsyncIterator())

Creates the asynchronous iterator that produces elements of this
asynchronous sequence.

[`AsyncIterator`](/documentation/Swift/AsyncSequence/AsyncIterator)

The type of asynchronous iterator that produces elements of this
asynchronous sequence.

[`AsyncIteratorProtocol`](/documentation/Swift/AsyncIteratorProtocol)

A type that asynchronously supplies the values of a sequence one at a
time.

[`Element`](/documentation/Swift/AsyncSequence/Element)

The type of element produced by this asynchronous sequence.

### Finding Elements

[`contains(_:)`](/documentation/Swift/AsyncSequence/contains(_:))

Returns a Boolean value that indicates whether the asynchronous sequence
contains the given element.

[`contains(where:)`](/documentation/Swift/AsyncSequence/contains(where:))

Returns a Boolean value that indicates whether the asynchronous sequence
contains an element that satisfies the given predicate.

[`allSatisfy(_:)`](/documentation/Swift/AsyncSequence/allSatisfy(_:))

Returns a Boolean value that indicates whether all elements produced by the
asynchronous sequence satisfy the given predicate.

[`first(where:)`](/documentation/Swift/AsyncSequence/first(where:))

Returns the first element of the sequence that satisfies the given
predicate.

[`min()`](/documentation/Swift/AsyncSequence/min())

Returns the minimum element in an asynchronous sequence of comparable
elements.

[`min(by:)`](/documentation/Swift/AsyncSequence/min(by:))

Returns the minimum element in the asynchronous sequence, using the given
predicate as the comparison between elements.

[`max()`](/documentation/Swift/AsyncSequence/max())

Returns the maximum element in an asynchronous sequence of comparable
elements.

[`max(by:)`](/documentation/Swift/AsyncSequence/max(by:))

Returns the maximum element in the asynchronous sequence, using the given
predicate as the comparison between elements.

### Selecting Elements

[`prefix(_:)`](/documentation/Swift/AsyncSequence/prefix(_:))

Returns an asynchronous sequence, up to the specified maximum length,
containing the initial elements of the base asynchronous sequence.

[`AsyncPrefixSequence`](/documentation/Swift/AsyncPrefixSequence)

An asynchronous sequence, up to a specified maximum length,
containing the initial elements of a base asynchronous sequence.

[`prefix(while:)`](/documentation/Swift/AsyncSequence/prefix(while:)-2xy95)

Returns an asynchronous sequence, containing the initial, consecutive
elements of the base sequence that satisfy the given predicate.

[`AsyncPrefixWhileSequence`](/documentation/Swift/AsyncPrefixWhileSequence)

An asynchronous sequence, containing the initial, consecutive
elements of the base sequence that satisfy a given predicate.

[`prefix(while:)`](/documentation/Swift/AsyncSequence/prefix(while:)-6yp5n)

Returns an asynchronous sequence, containing the initial, consecutive
elements of the base sequence that satisfy the given error-throwing
predicate.

[`AsyncThrowingPrefixWhileSequence`](/documentation/Swift/AsyncThrowingPrefixWhileSequence)

An asynchronous sequence, containing the initial, consecutive
elements of the base sequence that satisfy the given error-throwing
predicate.

### Excluding Elements

[`dropFirst(_:)`](/documentation/Swift/AsyncSequence/dropFirst(_:))

Omits a specified number of elements from the base asynchronous sequence,
then passes through all remaining elements.

[`AsyncDropFirstSequence`](/documentation/Swift/AsyncDropFirstSequence)

An asynchronous sequence which omits a specified number of elements from the
base asynchronous sequence, then passes through all remaining elements.

[`drop(while:)`](/documentation/Swift/AsyncSequence/drop(while:)-9sp3b)

Omits elements from the base asynchronous sequence until a given closure
returns false, after which it passes through all remaining elements.

[`AsyncDropWhileSequence`](/documentation/Swift/AsyncDropWhileSequence)

An asynchronous sequence which omits elements from the base sequence until a
given closure returns false, after which it passes through all remaining
elements.

[`drop(while:)`](/documentation/Swift/AsyncSequence/drop(while:)-67kgo)

Omits elements from the base sequence until a given error-throwing closure
returns false, after which it passes through all remaining elements.

[`AsyncThrowingDropWhileSequence`](/documentation/Swift/AsyncThrowingDropWhileSequence)

An asynchronous sequence which omits elements from the base sequence until a
given error-throwing closure returns false, after which it passes through
all remaining elements.

[`filter(_:)`](/documentation/Swift/AsyncSequence/filter(_:)-435af)

Creates an asynchronous sequence that contains, in order, the elements of
the base sequence that satisfy the given predicate.

[`AsyncFilterSequence`](/documentation/Swift/AsyncFilterSequence)

An asynchronous sequence that contains, in order, the elements of
the base sequence that satisfy a given predicate.

[`filter(_:)`](/documentation/Swift/AsyncSequence/filter(_:)-2cc0l)

Creates an asynchronous sequence that contains, in order, the elements of
the base sequence that satisfy the given error-throwing predicate.

[`AsyncThrowingFilterSequence`](/documentation/Swift/AsyncThrowingFilterSequence)

An asynchronous sequence that contains, in order, the elements of
the base sequence that satisfy the given error-throwing predicate.

### Transforming a Sequence

[`map(_:)`](/documentation/Swift/AsyncSequence/map(_:)-1q1k3)

Creates an asynchronous sequence that maps the given closure over the
asynchronous sequence’s elements.

[`AsyncMapSequence`](/documentation/Swift/AsyncMapSequence)

An asynchronous sequence that maps the given closure over the asynchronous
sequence’s elements.

[`map(_:)`](/documentation/Swift/AsyncSequence/map(_:)-70wgb)

Creates an asynchronous sequence that maps the given error-throwing
closure over the asynchronous sequence’s elements.

[`AsyncThrowingMapSequence`](/documentation/Swift/AsyncThrowingMapSequence)

An asynchronous sequence that maps the given error-throwing closure over the
asynchronous sequence’s elements.

[`compactMap(_:)`](/documentation/Swift/AsyncSequence/compactMap(_:)-gfdq)

Creates an asynchronous sequence that maps the given closure over the
asynchronous sequence’s elements, omitting results that don’t return a
value.

[`AsyncCompactMapSequence`](/documentation/Swift/AsyncCompactMapSequence)

An asynchronous sequence that maps a given closure over the asynchronous
sequence’s elements, omitting results that don’t return a value.

[`compactMap(_:)`](/documentation/Swift/AsyncSequence/compactMap(_:)-1f8zn)

Creates an asynchronous sequence that maps an error-throwing closure over
the base sequence’s elements, omitting results that don’t return a value.

[`AsyncThrowingCompactMapSequence`](/documentation/Swift/AsyncThrowingCompactMapSequence)

An asynchronous sequence that maps an error-throwing closure over the base
sequence’s elements, omitting results that don’t return a value.

[`AsyncFlatMapSequence`](/documentation/Swift/AsyncFlatMapSequence)

An asynchronous sequence that concatenates the results of calling a given
transformation with each element of this sequence.

[`AsyncThrowingFlatMapSequence`](/documentation/Swift/AsyncThrowingFlatMapSequence)

An asynchronous sequence that concatenates the results of calling a given
error-throwing transformation with each element of this sequence.

[`reduce(_:_:)`](/documentation/Swift/AsyncSequence/reduce(_:_:))

Returns the result of combining the elements of the asynchronous sequence
using the given closure.

[`reduce(into:_:)`](/documentation/Swift/AsyncSequence/reduce(into:_:))

Returns the result of combining the elements of the asynchronous sequence
using the given closure, given a mutable initial value.

### Adapting Textual Sequences

[`characters`](/documentation/Swift/AsyncSequence/characters)

A non-blocking sequence of `Characters` created by decoding the elements of `self` as UTF8.

  <doc://com.apple.documentation/documentation/Foundation/AsyncCharacterSequence>

[`unicodeScalars`](/documentation/Swift/AsyncSequence/unicodeScalars)

A non-blocking sequence of `UnicodeScalars` created by decoding the elements of `self` as UTF8.

  <doc://com.apple.documentation/documentation/Foundation/AsyncUnicodeScalarSequence>

[`lines`](/documentation/Swift/AsyncSequence/lines)

A non-blocking sequence of newline-separated `Strings` created by decoding the elements of `self` as UTF8.

  <doc://com.apple.documentation/documentation/Foundation/AsyncLineSequence>



---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)