Anyone have a library or snippet they use to conve...
# javascript
a
Anyone have a library or snippet they use to convert `suspend fun`s to a cancelable
Promise
?
t
сс @hfhbd
🙏 1
r
There's a promise builder on
CoroutineScope
. Would using that and cancelling the scope meet your cancellability needs?
t
Copy code
GlobalScope.async {
      // ...
}.asPromise()
Ah, but not sure what you mean by "cancelable". I don't see cancelation facility in Promise API.
a
Yeah I've definitely seen
asPromise
and
.promise
! I think what we'd ideally want is some nice way to cancel the underlying scope that looks like idiomatic jS, rather than having to keep track of the
Promise
and and scope the same time (similar to the way that most folks currently wrap coroutines in a suspendwrapper class for native)
r
If Kotlin weren't involved, what would the idiomatic JS you have in mind look like? You can probably wrap a
CoroutineScope
or a
CompletableDeferred
or something in a container with whatever JS API you're looking for, but I don't know what that is
h
Like @Tomasz Krakowiak already said, promises are not cancelable. You can only implement cancelation by yourself: Add some extra parameter, cancel the inner suspend function and reject the promise.
a
Ah yeah I guess that lack of standardization for Promise cancelation is the problem It looks like
AbortController
is the standard way to do this with
fetch
but its got experimental support…