anyone know how to migrate this code from <https:/...
# codereview
d
anyone know how to migrate this code from https://github.com/Kotlin/KEEP/blob/master/proposals/coroutines.md ?
Copy code
fun <T> future(context: CoroutineContext = CommonPool, block: suspend () -> T): CompletableFuture<T> =
        CompletableFutureCoroutine<T>(context).also { block.startCoroutine(completion = it) }

class CompletableFutureCoroutine<T>(override val context: CoroutineContext) : CompletableFuture<T>(), Continuation<T> {
    override fun resumeWith(result: Result<T>) {
        result
                .onSuccess { complete(it) }
                .onFailure { completeExceptionally(it) }
    }
}
suspend fun <T> CompletableFuture<T>.await(): T =
        suspendCoroutine { cont: Continuation<T> ->
            whenComplete { result, exception ->
                if (exception == null) // the future has been completed normally
                    cont.resume(result)
                else // the future has completed with an exception
                    cont.resumeWithException(exception)
            }
        }
I mean, is it correct apart from the
CommonPool
part? should I just replace
CommonPool
with
EmptyCoroutineContext
?
g
Why do you use custom future implementation instead of provided by kotlinx.coroutines java8 adapter? This implementation from KEEP is just an example if you want to use in own code, ai would just use kotlinx.coroutines
d
that's part of my old code , i removed it in my new code but currently that part is shelved - so it get it to compile i still have that there
g
Also empty coroutine context doesn't have sense in this case
Do you use Kotlinx.coroutines?
d
i just upgraded to 1.0,1 last week
g
If so,just use Dispatcher.Default to make it work instead of common pool
d
ok i will try that, thanks
g
But I would just remove this function and use kotlinx.coroutines future with structured concurrency https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-jdk8/kotlinx.coroutines.future/kotlinx.coroutines.-coroutine-scope/future.html