In Kotlin, all Coroutines run inside a CoroutineScope
.
A scope controls the lifetime of coroutines through its job. When you cancel the job of a scope, it cancels all coroutines started in that scope.
A context that enforces cancellation and other rules to its children (Jobs) and their children recursively.
Functions used to create new coroutines such as launch()
and async()
extend CoroutineScope
.
💡 The pattern of async-await from JS is similar to coroutines. The suspend keyword is similar to
async
. In Kotlin,await()
is implicit when calling asuspend
function.
Kotlin has a method Deferred.await()
that is used to wait for the result from a coroutine started with the async
builder.