which coroutine scope do you use inside your brows...
# javascript
b
which coroutine scope do you use inside your browser, for instance to load data from an external API or react to a button click? IO for the http request and Main for the button click?
a
You can use the
MainScope
for most of your coroutine needs.
Copy code
val mainScope = MainScope()

fun load() {
  mainScope.launch {
    // your async logic here
  }
}
c
In JS, it doesn't really matter which dispatcher you use, since there's only one thread anyway.
b
so no service worker dispatcher?
c
Service workers are bit more complex than that 😕 Service workers do not share memory with the main website thread, so you cannot just send them code to execute, so it's not possible (to my knowledge!) to implement them as a dispatcher. What people usually do is create another Kotlin/JS module which is published as a service worker by the main module, then share data between them through indexdb or similar. This is quite tedious to do by hand, so multiple libraries exist to help with this (for example with #kobweb: https://github.com/varabyte/kobweb?tab=readme-ov-file#creating-a-kobweb-worker)
t
At one point there was a web worker proposal being worked on https://github.com/Kotlin/KEEP/blob/master/notes/web-workers.md But I imagine wasm with the potential upcoming multi threading support might be a better bet.