Skip to content

Instantly share code, notes, and snippets.

@MovileGente
Created March 21, 2019 14:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MovileGente/0e3b3856c47009554d922756202f922f to your computer and use it in GitHub Desktop.
Save MovileGente/0e3b3856c47009554d922756202f922f to your computer and use it in GitHub Desktop.
// 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