https://kotlinlang.org logo
#coroutines
Title
# coroutines
d

Dias

08/13/2019, 1:37 PM
I have an api that provides 2 ways of calling a method. One returns Future (plain java future) and another one accepts callback. Which way is better to convert it to suspendable function? 1. Wrap call into withContext(Distpatcher.IO) { future.get() } 2. Wrap callback with a suspendCoroutine{ } method I prefer second option, because it looks more idiomatic, but withContext looks cleaner for a person less familiar with coroutines
g

gildor

08/13/2019, 2:02 PM
100% suspendCoroutine
a person less familiar with coroutines
But no need to expose it, you just should write an adapter for this API that based on callbacks, so user of this API can just call it, without additional wrapper
d

Dias

08/13/2019, 2:17 PM
that's true
👍
3 Views