Combine convert Just to AnyPublisher

Viewed 3488

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?

1 Answers

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

Just([Int]())
    .setFailureType(to: Error.self)
    .eraseToAnyPublisher()
Related