When using suspending functions as class functions...
# javascript
e
When using suspending functions as class functions, how do you expose them to JS as promises? Is creating wrappers the only way?
a
yes wrapping is the only way
e
@andylamax ouch, that's so redundant. I can't even reuse base interfaces as their methods are declared as suspendable
a
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
@andylamax uh, callbacks by default seems a good idea, but I don't understand the extension functions part
a
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)
  }
}
e
Thanks! I'll think about this!