withContext

suspend fun <T> withContext(
    context: CoroutineContext,
    start: CoroutineStart = CoroutineStart.DEFAULT,
    block: suspend () -> T
): T
(source)

Calls the specified suspending block with a given coroutine context, suspends until it completes, and returns the result.

This function immediately applies dispatcher from the new context, shifting execution of the block into the different thread inside the block, and back when it completes. The specified context is added onto the current coroutine context for the execution of the block.

An optional start parameter is used only if the specified context uses a different CoroutineDispatcher than a current one, otherwise it is ignored. By default, the coroutine is immediately scheduled for execution and can be cancelled while it is waiting to be executed and it can be cancelled while the result is scheduled to be processed by the invoker context. Other options can be specified via start parameter. See CoroutineStart for details. A value of CoroutineStart.LAZY is not supported and produces IllegalArgumentException.