im working with an async library that returns a Co...
# announcements
x
im working with an async library that returns a CompletableFuture; how could i convert it into a coroutine? (like Mono.fromFuture() does)
x
@Adam Powell yes, that is useful, but what if I don't want to await it yet?
what if i want await to run in another layer?
a
are you asking how to get a
Deferred
from it? (the
.asDeferred
method documented on that page?)
x
that's it, thank you
👍 1
still new to this so im lost on some keywords like deferred, too used to javascript async
@Adam Powell last question, what if you want to do something on a deferred function once its done, without actively awaiting it?
like an .onComplete{ println(it) }
a
if that's what you're looking to do, I don't think you want to convert it to a
Deferred
after all. It sounds like you want something more like:
Copy code
launch {
  val result = future.await()
  println(result)
}
x
but in that case you're awaiting for it on that step
i don't want to actively await it, just that once it completes it does something in particular with it
so that if the caller of the function actively awaits for it, then the data structure it receives will be what i expect it to be
an example in pseudocode:
Copy code
fun parentFunction(){
  val test = childFunction.await()
  return test + test
}

fun childFunction(){
  (outside function that i have no control of).onComplete{ it.toNumber() }
}
a
that is not the idiomatic way to use coroutines. Suspend functions naturally await their results. The
.await
extension brings
CompletableFuture
usage into this model so that you can expose
suspend
functions to the rest of your code and use the structured concurrency model.
If a caller does not want to actively await the result of a suspend function, it is up to that caller to
launch {}
or
async {}
it
x
alright, thank you