Hi guys, I’m working with coroutine and Either and...
# arrow
h
Hi guys, I’m working with coroutine and Either and I faced a problem with the flatMap of either.
Copy code
suspend fun findLocation() = coroutineScope {
        locationDelegate.checkSettings().flatMap {
            locationDelegate.getLocation()
        }
    }
In this sample, I can’t call
locationDelegate.getLocation()
since the
getLocation()
is a suspend function. How can I handle suspend functions in
flatMap
since it is not an inline function?
r
Either.flatMap can only compute a pure function since it's strict and your coroutine function is suspended. You'd need to use Either.catch or resolve the result before calling flatMap. In practice what you need instead of Either is to use IO which is the only data type that can deal with effects
🤔 1
h
Thanks for your reply. I will try to change the code to use
IO
r
If you end up using IO then you don't need the scopes as IO handles error and autocancels automatically
If you need help translating a small snippets we can help showing how that looks like in IO.
h
Thanks but first of all I prefer to read something more about IO to under how to face it in a proper way
💪🏽 1