`IO.unsafeRunSync` has this in the comment above i...
# arrow
s
IO.unsafeRunSync
has this in the comment above it
Copy code
* **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
Copy code
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, jooq
s
Hei. I asked this too sometime back. Maybe this will help ? https://kotlinlang.slack.com/archives/C5UPMM0A0/p1579788953131700
👍 1
s
why would not I just do the
Copy code
try {
//call db and return Right(result)
} catch (e) {
// inspect e and return appropriate Left(MyAppRelevantError)
}
instead of
Copy code
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.
s
If you’re using
IO
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.
Amongst some of the benefits of
IO
. Using
suspend
with
Either
can achieve the same benefits.
s
One thing, though. If you want to use plain
Either
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).
r
@streetsofboston this may change with the new suspend support @simon.vergauwen is working on for Fx
👍 3