https://kotlinlang.org logo
#coroutines
Title
# coroutines
a

arocnies

12/01/2017, 4:31 AM
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

gildor

12/01/2017, 5:17 AM
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

pdvrieze

12/01/2017, 10:05 AM
@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

gildor

12/01/2017, 10:26 AM
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)
5 Views