async
creates a coroutine and runs in the Coroutine context, inherited from a CoroutineScope
, additional context elements can be specified with context argument. If the context does not have any dispatcher nor any other ContinuationInterceptor
, then Dispatchers.Default
is used.
If Dispatchers.Default
is used then whatever function you invoke in async
builder it will run asynchronously. To switch contexts you can use withContext
function:
async {
delay(5000)
withContext(Dispatchers.Main) {
// if we use `Dispatchers.Main` as a coroutine context next two lines will be executed on UI thread.
doSomething()
doAnotherThing()
}
}
If async
runs in the Dispatchers.Main
context you don't need to switch contexts:
var job: Job = Job()
var scope = CoroutineScope(Dispatchers.Main + job)
scope.async {
delay(5000) // suspends the coroutine without blocking UI thread
// runs on UI thread
doSomething()
doAnotherThing()
}
Note: async
is mainly used for parallel executions. To start a simple coroutine launch
builder is used. So you can replace all async
functions in those examples on launch
function.
Also to run coroutine with async
builder you need to call await()
function on Deferred
object which is returned by async
function. Here is some additional info.