<!--
{
  "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/drop(while:)-67kgo",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Swift"
    ],
    "preciseIdentifier" : "s:Sci12_ConcurrencyE4drop5whileAA30AsyncThrowingDropWhileSequenceVyxGSb7ElementQzYaKc_tF"
  },
  "title" : "drop(while:)"
}
-->

# drop(while:)

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

```
@preconcurrency func drop(while predicate: @escaping @Sendable (Self.Element) async throws -> Bool) -> AsyncThrowingDropWhileSequence<Self>
```

## Parameters

`predicate`

An error-throwing closure that takes an element as
a parameter and returns a Boolean value indicating whether to drop the
element from the modified sequence.

## Return Value

An asynchronous sequence that skips over values until the
provided closure returns `false` or throws an error.

## Discussion

Use `drop(while:)` to omit elements from an asynchronous sequence until
the element received meets a condition you specify. If the closure you
provide throws an error, the sequence produces no elements and throws
the error instead.

In this example, an asynchronous sequence called `Counter` produces `Int`
values from `1` to `10`. The predicate passed to the `drop(while:)`
method throws an error if it encounters an even number, and otherwise
returns `true` while it receives elements less than `5`. Because the
predicate throws when it receives `2` from the base sequence, this example
throws without ever printing anything.

```
do {
    let stream = Counter(howHigh: 10)
        .drop {
            if $0 % 2 == 0 {
                throw EvenError()
            }
            return $0 < 5
        }
    for try await number in stream {
        print(number)
    }
} catch {
    print(error)
}
// Prints "EvenError()"
```

After the predicate returns `false`, the sequence never executes it again,
and from then on the sequence passes through elements from its underlying
sequence. A predicate that throws an error also never executes again.

---

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)