lazyAsync
Creates a lazily started coroutine which runs the provided suspend function. The result of the suspend function is wrapped in a Deferred object and is computed only upon the first invocation of Deferred.await or Deferred.join.
Example usage:
suspend fun fetchUser(): User = userService.getUser(userId)
coroutineScope {
val userAsync by lazyAsync { fetchUser() }
// The fetchUser() function will not be called until userDeferred.await() or userDeferred.join() is called.
val user = userAsync.await() // fetches the user asynchronously when awaited
}
Content copied to clipboard
Extension property on a suspend function that allows it to be called asynchronously. The result of the suspend function is wrapped in a Deferred object and is computed only upon the first invocation of Deferred.await or Deferred.join.
Example usage:
suspend fun calculate(): Int = 2 + 2
val result = calculate().lazyAsync
println(result.await()) // prints 4
Content copied to clipboard