Saša Šijak
05/16/2020, 4:45 AMIO.unsafeRunSync
has this in the comment above it
* **NOTE** this function is intended for testing, it should never appear in your mainline production code!
Why is that. How would you implement the case where in a typical web app I have the following:
api request -> http handler triggered -> it calls the service method -> service method calls DB service
And I want to wrap the call to the DB service in the equivalent of
Try {
//db call here
}.toEither {
//handle db errors
}
I want to use Either early and at the boundary of my core logic, so I can model my function returns and errors as I want and translate exceptions to my errors. Also I am not running in the coroutine context. For example, stack is http4k, jooqSatyam Agarwal
05/16/2020, 5:59 AMSaša Šijak
05/16/2020, 8:31 AMtry {
//call db and return Right(result)
} catch (e) {
// inspect e and return appropriate Left(MyAppRelevantError)
}
instead of
IO {
//call db
}.atempt().unsafeRunSync().mapLeft {//transform the exceptios to my app errors }
In my case where I am running in a threaded environment and need results of the db action right away, I dont see a point in using IO.simon.vergauwen
05/16/2020, 9:46 AMIO
in this manner there is no point in using it. The point of using IO
are any effect type is that it allows you to pass around effect full computations as values. This allows for very powerful concurrency, resource safety & cancellation guarantees that are otherwise hard and error prone to encode in your business code.simon.vergauwen
05/16/2020, 9:47 AMIO
. Using suspend
with Either
can achieve the same benefits.streetsofboston
05/16/2020, 10:49 AMEither
with suspend
, the fact that `Either`'s map
, and other operators/methods/functions can not take a suspend
lambda may be problematic (you can't call a suspend fun
in the `map`'s lambda).raulraja
05/16/2020, 10:56 AM