useAsync

fun useAsync(fn: SuspendAsyncFn): () -> Unit

This hook function accepts a suspend function as a parameter and returns a function(use to execute suspend function). When you call the execution function, the suspend function will be executed within the coroutine scope of the current component.

// Declare an suspend lambda like a normal lambda
val async = useAsync {
delay(1.seconds)
setState(state + 1)
}
// easy to use
async()

Return

Receiver

Parameters

fn

fun useAsync(): AsyncRunFn

This is a hook function that simplifies the use of coroutine scope. It's usage similar to the run function. Equivalent to scope.launch { }

val asyncRun = useAsync()
asyncRun {
// do something
}

Return