What's the best way to wrap a coroutine in `IO`? (...
# arrow
m
What's the best way to wrap a coroutine in
IO
? (I'm a bit lost in the forest of
IO
,
Async
and
fx
constructors and combinators). Take this example:
Copy code
suspend fun getCoroutines(internalHotelId: Int): Either<Throwable, CustomerFeedback?> { ... }
I'd like to execute it asynchronously. This is my take:
Copy code
fun getIOAsync(internalHotelId: Int): IO<CustomerFeedback?> =
    IO.async().async { callback ->
      GlobalScope.launch {
        val result = GlobalScope.async { getCoroutines(internalHotelId) }.await()
        callback.invoke(result)
      }
    }
  }
Is this the correct way? Are there any other ways? Can this be achieved with
fx
and
effect
?
r
hi @Marcin Gryszko
Copy code
fun getIOAsync(internalHotelId: Int): IO<CustomerFeedback?> = fx {
  val suspendedEffect = effect { getCoroutines(internalHotelId) }
  val fiber = !NonBlocking.startFiber(suspendedEffect)
  !fiber.join()
}
Or after 0.10.0 which we are working towards right now you can also do:
Copy code
IO.effect(NonBlocking) { getCoroutines(internalHotelId) }
where NonBlocking is the ctx you want to run your fiber on.
m
Thanks, I'll try it
r
no problem, let us know if you run into any issues 🙂