Recommendations for using coroutines in Kotlin JS

Indeed, you can lazily launch coroutines with kotlinx-coroutines-core-js. It has two ways to start a coroutine: launch (returns a Job) is for coroutines that are not expected to return a result, but simply represent some background activity, while async (return a Deferred) is for coroutines that you plan to retrieve result later. You kind find about the difference here: asynchronous - What is the difference between launch/join and async/await in Kotlin coroutines - Stack Overflow

Both accept an optional start = CoroutineStart.LAZY parameter. In case of a launch the coroutine is actually started when you invoke Job.start(), while with Deferred, in addition to that, it is started on a first Deferred.await() invocation.

Additional features that you might find useful include cancellation of coroutines and integration with JS Promise.

1 Like