---
title: "prefix(_:)"
framework: swift
role: symbol
role_heading: Instance Method
path: "swift/taskgroup/prefix(_:)"
---

# prefix(_:)

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

## Declaration

```swift
func prefix(_ count: Int) -> AsyncPrefixSequence<Self>
```

## Parameters

- `count`: The maximum number of elements to return. The value of count must be greater than or equal to zero.

## Return Value

Return Value An asynchronous sequence starting at the beginning of the base sequence with at most count elements.

## Discussion

Discussion Use prefix(_:) to reduce the number of elements produced by the asynchronous sequence. In this example, an asynchronous sequence called Counter produces Int values from 1 to 10. The prefix(_:) method causes the modified sequence to pass through the first six values, then end. for await number in Counter(howHigh: 10).prefix(6) {     print(number, terminator: " ") } // Prints "1 2 3 4 5 6 " If the count passed to prefix(_:) exceeds the number of elements in the base sequence, the result contains all of the elements in the sequence.

## See Also

### Accessing an Asynchronous Sequence of Results

- [makeAsyncIterator()](swift/taskgroup/makeasynciterator().md)
- [allSatisfy(_:)](swift/taskgroup/allsatisfy(_:).md)
- [compactMap(_:)](swift/taskgroup/compactmap(_:)-944od.md)
- [compactMap(_:)](swift/taskgroup/compactmap(_:)-7mgj1.md)
- [contains(_:)](swift/taskgroup/contains(_:).md)
- [contains(where:)](swift/taskgroup/contains(where:).md)
- [drop(while:)](swift/taskgroup/drop(while:).md)
- [dropFirst(_:)](swift/taskgroup/dropfirst(_:).md)
- [filter(_:)](swift/taskgroup/filter(_:).md)
- [first(where:)](swift/taskgroup/first(where:).md)
- [flatMap(_:)](swift/taskgroup/flatmap(_:)-vhi3.md)
- [map(_:)](swift/taskgroup/map(_:)-58nsr.md)
- [map(_:)](swift/taskgroup/map(_:)-4a4kq.md)
- [max()](swift/taskgroup/max().md)
- [max(by:)](swift/taskgroup/max(by:).md)
