---
title: "filter(_:)"
framework: swift
role: symbol
role_heading: Instance Method
path: "swift/asyncsequence/filter(_:)-435af"
---

# filter(_:)

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

## Declaration

```swift
@preconcurrency func filter(_ isIncluded: @escaping @Sendable (Self.Element) async -> Bool) -> AsyncFilterSequence<Self>
```

## Parameters

- `isIncluded`: A closure that takes an element of the asynchronous sequence as its argument and returns a Boolean value that indicates whether to include the element in the filtered sequence.

## Return Value

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

## Discussion

Discussion In this example, an asynchronous sequence called Counter produces Int values from 1 to 10. The filter(_:) method returns true for even values and false for odd values, thereby filtering out the odd values: let stream = Counter(howHigh: 10)     .filter { $0 % 2 == 0 } for await number in stream {     print(number, terminator: " ") } // Prints "2 4 6 8 10 "

## See Also

### Excluding Elements

- [dropFirst(_:)](swift/asyncsequence/dropfirst(_:).md)
- [AsyncDropFirstSequence](swift/asyncdropfirstsequence.md)
- [drop(while:)](swift/asyncsequence/drop(while:)-9sp3b.md)
- [AsyncDropWhileSequence](swift/asyncdropwhilesequence.md)
- [drop(while:)](swift/asyncsequence/drop(while:)-67kgo.md)
- [AsyncThrowingDropWhileSequence](swift/asyncthrowingdropwhilesequence.md)
- [AsyncFilterSequence](swift/asyncfiltersequence.md)
- [filter(_:)](swift/asyncsequence/filter(_:)-2cc0l.md)
- [AsyncThrowingFilterSequence](swift/asyncthrowingfiltersequence.md)
