Kotlin coroutines onErrorReturn (RX Java counterpart)

164 views Asked by At

In RX Java there is a possibility to provide a default value in case of error, with a variety of onError... methods.

What is the counterpart in Kotlin coroutines?

I understand I can wrap code inside launch or await calls in try...catch, but maybe there is a better solution?

What I need to convert this RX code to coroutines:

Single.zip(
    call1().onErrorResumeNext().observeOn(Dispatchers.IO),
    call2().onErrorReturnItem(DummyObject()).observeOn(Dispatchers.IO),
    call3().observeOn(Dispatchers.IO),
    result1, result2, result3 -> [zipper func]
)

And I don't see an attractive solution for that.

1

There are 1 answers

5
Jemshit On

You can do all in catch{} operator:

  • Rethrow usingĀ throw
  • emit new value
  • Ignore/log

Conceptually, the action of catch operator is similar to wrapping the code of upstream flows with try { ... } catch (e: Throwable) { action(e) }