kotlin coroutines books 2020 code example
Example 1: kotlin coroutine scope
CoroutineScope
To start coroutine scope you can:
Use GlobalScope that has empty coroutine context.
Implement CoroutineScope interface.
Create a scope from a context:
with(CoroutineScope(context = context)) { ... }
Example 2: kotlin coroutine builders
launch - Launches new coroutine without blocking
current thread and returns a reference to the coroutine
as a Job.
runBlocking - Runs new coroutine and blocks current
thread interruptible until its completion.
async - Creates new coroutine and returns its future
result as an implementation of Deferred.
withContext - Change a coroutine context for some
block.