Is there any way (even if dirty) to call a `suspen...
# javascript
m
Is there any way (even if dirty) to call a
suspend fun
from a regular
fun
? I only need that for unit testing a single function and can be sure that the function doesn’t actually suspend nor launch any other coroutines.
r
You mean without
GlobalScope.lauch {}
?
m
Yes without, synchronously
r
The early versions of kotlinx.coroutines for Kotlin/JS contained an implementation of
runBlocking {}
. It was removed because it was "dirty", but I remember it was working somehow 🙂. Maybe you can search in the old sources.
m
hehe I can try but I guess it used compiler-internal voodoo 😄
c
Since JS doesn’t have a way of actually blocking a thread, there’s not many good options. There’s no synchronous sleep, so polling would have to just run CPU cycles. Wrapping
.launch { }
in a Promise seems to be the most common way of unit testing coroutines, since both Mocha and Karma (the kotlin/JS test runners) support tests that return promises
m
Thanks 🙂 Like like I’d also have to add
BlockingEventLoop
,
BlockingCoroutine
and all sorts of things 😅