Chapter 4. Finding Values `last(where:)`

example(of: "last(where:)") {
  enum MyError: Error {
    case test
  }
  let subject = PassthroughSubject<Int, MyError>()

  subject
    .eraseToAnyPublisher()
    .last(where: { $0 % 2 == 0 })
    .sink(receiveCompletion: { print("Completed with: ", $0) },
          receiveValue: { print($0) })
    .store(in: &subscriptions)

  subject.send(1)
  subject.send(2)
  subject.send(4)
  subject.send(completion: .failure(MyError.test))
}

Output:

——— Example of: last(where:) 2 ———
Completed with:  failure(...).MyError.test)

How can I receive the value 4?

This should work to get the last emitted value

subject
    .catch { _ in Empty() }
    .last(where: { $0 % 2 == 0 })
    .sink { print($0) }