Has anyone implemented a `runBlocking {...}` for K...
# coroutines
a
Has anyone implemented a
runBlocking {...}
for Kotlin/JS?
launch {...}
is relatively straight forward but I just can't seem to figure out how to properly block for a coroutine in JS.
g
I’m not sure that this is possible, because JS has only one thread (not including webworker) if you lock you cannot do anything else
p
@arocnies
runBlocking
doesn't do what you may think it does. It runs everything (except subcoroutines) on the same thread (the one that started it). In some ways this fits well with Javascript. You cannot actually wait for a coroutine completion in Javascript. What you do is use a callback on completion. Waiting for a coroutine amounts to either inside the coroutine just doing what you want with the result or starting a new coroutine that invokes await on the previous one.
g
runBlocking still could be useful on JS, you can just lock main app thread and wait coroutine, but it’s just impossible to do, as I understand because of single thread nature of JS, because if you locked main thread you cannot do anything else, even get result of some async callback (from worker or network request)