asyncMain

fun <T> CoroutineScope.asyncMain(context: CoroutineContext = EmptyCoroutineContext, start: CoroutineStart = CoroutineStart.DEFAULT, block: suspend CoroutineScope.() -> T): Deferred<T>

Creates a coroutine and returns its future result as an implementation of Deferred.

Uses the maindispatcher.

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.asyncDefault
import dispatch.core.asyncIO
import dispatch.core.asyncMain
import dispatch.core.asyncMainImmediate
import dispatch.core.asyncUnconfined
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"

    asyncMain {

      dispatcherName() shouldBe "main"
    }.join()
  } 
   //sampleEnd
}

See also

async

Sources

Link copied to clipboard