Hello guys, I need some help architecting a small ...
# coroutines
m
Hello guys, I need some help architecting a small library which uses coroutines and wraps some Java legacy async callback, methods should be called in sequence if the previous one is successful but if one fails it has a custom recovery action. • Which of those 2 approaches would be better? • Is there a better approach of both?
Copy code
scope.launch {
  //errors are propagated with resumeWithException
  try {
    library.step1()
    library.step2()
    val result3 = library.step3()
    library.step4(result3)
  } catch (error: ResultException) {
    when (error.code) {
      1 -> ....
      2 -> ....
      3 -> ....
      else -> ...
    }
  }
}

scope.launch {
  //errors are handled as state, propagated with resumeWith
  val result1 = library.step1()
  if (result1 is Result.Success) {
    val result2 = library.step2()
    if (result2 is Result.Success) {
      val result3 = library.step3()
      if (result3 is Result.Success) {
        library.step4(result3)
      } else {
        //handle error
      }
    } else {
      //handle error
    }
  } else {
    //handle error
  }
}