Mangat Rai Modi
Mangat Rai Modi

Reputation: 5706

RxJava2: Is chaining allowed inside mapper function?

In the code below function f returns Single<String>,

   Observable.map { line ->
              f(line).doOnError { e ->
                    println("Error:$e")
                }
        }
        .subscribe({ record -> println(record) }, { e -> println("Error2:$e") })

println("Error:$e") inside the map won't execute, however I will be able to get the error printed in the subscriber. It looks like that the chaining inside the mapper function in not allowed. Is it correct? If yes, why?

Edit: Also tried flatmap, but same result.

   Observable.flatmap { line ->
              f(line).toObservable().doOnError { e ->
                    println("Error:$e")
                }
        }
        .subscribe({ record -> println(record) }, { e -> println("Error2:$e") })

Upvotes: 0

Views: 158

Answers (1)

akarnokd
akarnokd

Reputation: 70017

This works as expected:

@Test
public void test() {
    Observable.just(1)
    .flatMap(v -> single(v)
            .toObservable()
            .doOnError(w -> System.out.println("Error2 " + w))
    )
    .subscribe(v -> System.out.println(v), e -> System.out.println("Error " + e));
}

Single<Integer> single(Integer v) {
    return Single.error(new IOException());
}

Prints:

Error2 java.io.IOException
Error java.io.IOException

Upvotes: 1

Related Questions