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

# tryPrefix(while:)

Republishes elements while an error-throwing predicate closure indicates publishing should continue.

## Declaration

```swift
func tryPrefix(while predicate: @escaping (Self.Output) throws -> Bool) -> Publishers.TryPrefixWhile<Self>
```

## Parameters

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

## Return Value

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

## Discussion

Discussion Use tryPrefix(while:) to emit values from the upstream publisher that meet a condition you specify in an error-throwing closure. The publisher finishes when the closure returns false. If the closure throws an error, the publisher fails with that error. struct OutOfRangeError: Error {}

let numbers = (0...10).reversed() cancellable = numbers.publisher     .tryPrefix {         guard $0 != 0 else {throw OutOfRangeError()}         return $0 <= numbers.max()!     }     .sink(         receiveCompletion: { print ("completion: \($0)", terminator: " ") },         receiveValue: { print ("\($0)", terminator: " ") }     )

// Prints: "10 9 8 7 6 5 4 3 2 1 completion: failure(OutOfRangeError()) "

## 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)
- [prefix(while:)](combine/publisher/prefix(while:).md)
- [prefix(untilOutputFrom:)](combine/publisher/prefix(untiloutputfrom:).md)
