---
title: "prefix(while:)"
framework: combine
role: symbol
role_heading: Instance Method
path: "combine/publisher/prefix(while:)"
---

# prefix(while:)

Republishes elements while a predicate closure indicates publishing should continue.

## Declaration

```swift
func prefix(while predicate: @escaping (Self.Output) -> Bool) -> Publishers.PrefixWhile<Self>
```

## Parameters

- `predicate`: A closure that takes an element as its parameter and returns a Boolean value that indicates whether publishing should continue.

## Return Value

Return Value A publisher that passes through elements until the predicate indicates publishing should finish.

## Discussion

Discussion Use prefix(while:) to emit values while elements from the upstream publisher meet a condition you specify. The publisher finishes when the closure returns false. In the example below, the prefix(while:) operator emits values while the element it receives is less than five: let numbers = (0...10) numbers.publisher     .prefix { $0 < 5 }     .sink { print("\($0)", terminator: " ") }

// Prints: "0 1 2 3 4"

## See Also

### Applying sequence operations to elements

- [drop(untilOutputFrom:)](combine/publisher/drop(untiloutputfrom:).md)
- [dropFirst(_:)](combine/publisher/dropfirst(_:).md)
- [drop(while:)](combine/publisher/drop(while:).md)
- [tryDrop(while:)](combine/publisher/trydrop(while:).md)
- [append(_:)](combine/publisher/append(_:)-1qb8d.md)
- [append(_:)](combine/publisher/append(_:)-69sdn.md)
- [append(_:)](combine/publisher/append(_:)-5yh02.md)
- [prepend(_:)](combine/publisher/prepend(_:)-7wk5l.md)
- [prepend(_:)](combine/publisher/prepend(_:)-v9sb.md)
- [prepend(_:)](combine/publisher/prepend(_:)-5dj9c.md)
- [prefix(_:)](combine/publisher/prefix(_:).md)
- [tryPrefix(while:)](combine/publisher/tryprefix(while:).md)
- [prefix(untilOutputFrom:)](combine/publisher/prefix(untiloutputfrom:).md)
