<!--
{
  "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" : "Combine",
  "identifier" : "/documentation/Combine/Publisher/prefix(while:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Combine"
    ],
    "preciseIdentifier" : "s:7Combine9PublisherPAAE6prefix5whileAA10PublishersO11PrefixWhileVy_xGSb6OutputQzc_tF"
  },
  "title" : "prefix(while:)"
}
-->

# prefix(while:)

Republishes elements while a predicate closure indicates publishing should continue.

```
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

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

## Discussion

Use [`prefix(while:)`](/documentation/Combine/Publisher/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:)`](/documentation/Combine/Publisher/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"
```

---

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)