# Combine map with switchToLatest confuse

**URL:** <https://forums.swift.org/t/combine-map-with-switchtolatest-confuse/51445>\
**Category:** Core Libraries\
**Tags:** combine\
**Created:** [August 24, 2021, 2:58am UTC](https://forums.swift.org/t/combine-map-with-switchtolatest-confuse/51445 "2021-08-24T02:58:29Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![douknow](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/douknow/32/18980_2.png) [@douknow](https://forums.swift.org/u/douknow)\
**Post date:** [August 24, 2021, 2:58am UTC](https://forums.swift.org/t/combine-map-with-switchtolatest-confuse/51445/1 "2021-08-24T02:58:29Z")

</div>

I have a source publisher, and have an another publisher rely source publisher.  
This is playground test code for my situation:

```swift
import Foundation
import Combine
import PlaygroundSupport

PlaygroundPage.current.needsIndefiniteExecution = true

typealias Image = Int

enum NetError: Error {
    case invalidImage
}

func convertImageToVideo(_ image: Image) -> AnyPublisher<Image, NetError> {
    Future { promise in
        DispatchQueue.global().asyncAfter(deadline: .now() + 1) {
            if image == 20 {
                promise(.failure(.invalidImage))
            } else {
                promise(.success(image))
            }
        }
    }
    .eraseToAnyPublisher()
}

var image = PassthroughSubject<Image, NetError>()

let subscription = image
    .map { image in
        convertImageToVideo(image)
    }
    .switchToLatest()
    .sink { completion in
        if case let .failure(error) = completion {
            print("Receive error: \(error)")
        }
    } receiveValue: { video in
        print("Receive new video: \(video)")
    }

image.send(0)
image.send(20)
image.send(40)

DispatchQueue.main.async {
    image.send(20)
}

DispatchQueue.main.asyncAfter(deadline: .now() + 4) {
    print("Send 50 into image.")
    image.send(50)
}

```

But I only receive one error in console:

```swift
Receive error: invalidImage

```

This is not ideal, I want continue receive value even if `convertImageToVideo` method occur an error.  
So I change code:

```swift
import Foundation
import Combine
import PlaygroundSupport

PlaygroundPage.current.needsIndefiniteExecution = true

typealias Image = Int

enum NetError: Error {
    case invalidImage
}

func convertImageToVideo(_ image: Image) -> AnyPublisher<Image, NetError> {
    Future { promise in
        DispatchQueue.global().asyncAfter(deadline: .now() + 1) {
            if image == 20 {
                promise(.failure(.invalidImage))
            } else {
                promise(.success(image))
            }
        }
    }
    .eraseToAnyPublisher()
}

var image = PassthroughSubject<Image, NetError>()

let subscription = image
    .map { image -> AnyPublisher<Result<Image, NetError>, Never> in
        convertImageToVideo(image)
            .map { video in
                .success(video)
            }
            .catch({ error in
                Just(.failure(error))
            })
            .eraseToAnyPublisher()
    }
    .switchToLatest()
    .sink { completion in
        if case let .failure(error) = completion {
            print("Receive error: \(error)")
        }
    } receiveValue: { video in
        print("Receive new video: \(video)")
    }

image.send(0)
image.send(20)
image.send(40)

DispatchQueue.main.async {
    image.send(20)
}

DispatchQueue.main.asyncAfter(deadline: .now() + 4) {
    print("Send 50 into image.")
    image.send(50)
}

```

This time output is ideal:

```swift
Receive new video: failure(__lldb_expr_64.NetError.invalidImage)
Send 50 into image.
Receive new video: success(50)

```

But the error not come from `completion` closure, instead of it come from `new value` closure, I must handle error from `complete` closure and `new value` closure.  
Anyone has good idea? Thanks!
