withDefault

suspend fun <T> withDefault(context: CoroutineContext = EmptyCoroutineContext, block: suspend CoroutineScope.() -> T): T

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

Uses the defaultdispatcher.

The specific dispatcherProvider instance to be used is determined after the context parameter has been folded into the receiver's context.

The selected DispatcherProvider is essentially the first non-null result from:

Samples

import dispatch.core.withDefault
import dispatch.core.withIO
import dispatch.core.withMain
import dispatch.core.withMainImmediate
import dispatch.core.withUnconfined
import dispatch.internal.test.Sample5
import dispatch.internal.test.dispatcherName
import dispatch.internal.test.someDispatcherProvider
import io.kotest.matchers.shouldBe
import kotlinx.coroutines.runBlocking
fun main() { 
   //sampleStart 
   runBlocking(someDispatcherProvider) {

    dispatcherName() shouldBe "runBlocking thread"

    withDefault {

      dispatcherName() shouldBe "default"
    }
  } 
   //sampleEnd
}

See also

withContext

Sources

Link copied to clipboard