Hi! Do you have any tips to handle Promise cancell...
# javascript
g
Hi! Do you have any tips to handle Promise cancellation? I've suspend methods in Kotlin code calling Typescript code that I want to be able to cancel, some of this code use
fetch
and so requires AbortController/AbortSignal.
t
AFAIK in react-query they have required logic
g
Do you mean ReactQuery from JS? Looks like they use AbortSignal too, I just don't know how to plug that properly on a coroutines. To be more specific, I'm currently at this point
Copy code
suspend fun myWrappedMethod() = GlobalScope.promise {
    val abortController = AbortController()
    val signal = abortController.signal
    val signalContainer = { signal } // Pseudo code, I presume I'll require a data class to match the typical usage
    theTypescriptMethod(signalContainer)
}
I can declare external for AbortController/AbortSignal, but I've to plug a call to
abortController.abort()
when the coroutines is cancelled. I can check from the coroutineContext the current state (isActive) but it means I'll need to use setTimeout every 100ms 😨 or is there a way to plug a callback on the cancellation in a better way?
t
cancellation == reject?
🙏 1
g
cancellation == reject?
No I'm thinking about Kotlin calling myWrapedMethod() (a suspend method) in a coroutine scope, and then cancelling the scope/job. I think it should trigger the AbortController.abort() method so that the webbrowser fetch method is cancelled. Also heavy code in the typescript method should be cancellation cooperative by checking the AbortSignal.isAborted frequently and stop working if required (by throwing an exception maybe?).
About the kotlin-wrappers, I feel like
onabort
should be a var if we want to attach a kotlin callback on it. (I copy-pasted the code so not a problem for me.)
👍 1
t
K 1
🙏 1
Fix released in
pre.300