Can I use coroutines in Kotlin when targeting JavaScript?
Asked Answered
C

1

7

Is it possible to launch any coroutines when targeting JavaScript? It doesn't like any of the usual ways like async{}

Example on: https://github.com/thigg/no-kotlinx-in-js

Czarina answered 3/8, 2019 at 21:43 Comment(0)
I
6

Yes, you can. Take a look at example-frontend-js — frontend application written in Kotlin/JS that uses coroutines to implement animations in imperative style.

Don't forget to add a dependency on org.jetbrains.kotlinx:kotlinx-coroutines-core in your Gradle configs.

Inopportune answered 3/8, 2019 at 22:11 Comment(6)
Im on Kotlin 1.3.41. I created a Kotlin JS Project with intellij idea and it says there is no package import kotlinx.coroutines.*. Did I get any config wrong?Czarina
Can't spot differences so far, they seem simply to include the stdlib, nothing else. I'm doing the same as far as I can see. I will build a minimal failing example...Czarina
I created a minimal-not-working-example: github.com/thigg/no-kotlinx-in-js maybe you can help me to spot what I am doing wrong?Czarina
You don't have all the dependencies they have in a sample project. Take a look at parent's build.gradle: github.com/Kotlin/kotlinx.coroutines/blob/master/…. That allprojects configure additional dependencies and plugins.Inopportune
Yes, it was the missing import. Thanks. Will you update your answer?Czarina
The link in this answer is now broken. It used to be a helpful example. Where did it go?Joannjoanna

© 2022 - 2024 — McMap. All rights reserved.