When using suspending functions as class functions, how do you expose them to JS as promises?
Is creating wrappers the only way?
a
andylamax
12/26/2020, 7:34 PM
yes wrapping is the only way
e
Edoardo Luppi
12/26/2020, 7:36 PM
@andylamax ouch, that's so redundant.
I can't even reuse base interfaces as their methods are declared as suspendable
a
andylamax
12/26/2020, 7:42 PM
to combat that, make your base interfaces with callbacks, then add await as extension functions to your base interfaces. That way your code is easily usable all over
e
Edoardo Luppi
12/26/2020, 7:51 PM
@andylamax uh, callbacks by default seems a good idea, but I don't understand the extension functions part
a
andylamax
12/26/2020, 8:28 PM
Let me give you an exaple
Copy code
interface ThingGetter {
fun getThing(callback: (Thing)->Unit)
}
suspend fun ThingGetter.await() = suspendCancelableCoroutine {cont->
getThing{thing->
cont.resume(thing)
}
}