Question out of curiosity. In the JVM world a coro...
# coroutines
e
Question out of curiosity. In the JVM world a coroutine can be converted to a future using
CoroutineScope.future(...)
. Internally
future(...)
uses a
CompletableFutureCoroutine
which overrides
onCompleted
and
onCancelled
to be notified and complete or cancel the
Future
. In the JS world the
CoroutineScope.promise(...)
function first calls
async(...)
to get a
Deferred
value, and then listen for its completion via
invokeOnCompletion
. Why this implementation difference and not create a
PromiseCoroutine
in the same way as
CompletableFutureCoroutine
?
Is it only because of
Copy code
promise.asDynamic().deferred = this
which can then be used in
Promise<T>.asDeferred()
? Still have to encounter a place where that is useful, but it's just me obviously.
s
This is an interesting question! I'm wondering if it's just an accident with no deeper explanation behind it. I can only see one other notable difference, which is that
promise()
appears to allow lazy start, while
future()
doesn't. But that just makes me more confused, to be honest.
As far as I can tell, either version could happily be implemented via either approach.
gratitude thank you 1
e
I did experiment a little yesterday, trying to find differences. One notable difference is with my own
PromiseCoroutine
I shaved off 5 kb from the outputted coroutines JS file.
I'd be curious to see if
invokeOnCompletion
is less performant than simply implementing the two
Coroutine
functions. It seems more involved for sure, both in logic and on the amount of instantiated objects.
The problem is debugging K/JS coroutine code is still a problematic experience. Sometimes it just doesn't stop, or you can't step correctly.