17

How to convert Just<[Int]> to AnyPublisher<[Int], Error>. When I use eraseToAnyPublisher() the type is AnyPublisher<[Int], Never> which is not the same as AnyPublisher<[Int], Error>

For example I have a simple function which I want to mock temporary

func getAllIds() -> AnyPublisher<[Int], Error> {
    return Just<[Int]>([]).eraseToAnyPublisher()
}

Any ideas?

mkowal87
  • 596
  • 4
  • 19

1 Answers1

45

Use .setFailureType. The situation you are in is exactly what it is for:

Just([Int]())
    .setFailureType(to: Error.self)
    .eraseToAnyPublisher()
rob mayoff
  • 375,296
  • 67
  • 796
  • 848
Dmitriy Lupych
  • 609
  • 7
  • 9