suspendLazy

fun <T> suspendLazy(initializer: suspend () -> T): SuspendLazy<T>

Like lazy, but the initializer can suspend. Read the value with SuspendLazy.get.

Example

val database = suspendLazy { openDatabase("app.db") }

suspend fun records(): List<Record> = database.get().queryAll()
// The first get() opens the database.
// Every later get() returns it at once.

Watch out

Parameters

initializer

the code that makes the value. It runs again after each failure, and never again after the first success.