Created
March 21, 2019 14:37
-
-
Save MovileGente/0e3b3856c47009554d922756202f922f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// define uma função para criar um future | |
// exemplo de uso: | |
// val stringFuture = future { “yay” } | |
// println(stringFuture.get()) | |
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 resume(value: T) { | |
complete(value) | |
} | |
override fun resumeWithException(exception: Throwable) { | |
completeExceptionally(exception) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment