SuspendLazy

class SuspendLazy<T>

A lazy value whose initializer can suspend. Create one with suspendLazy.

The first get runs the initializer. Callers that call get at the same time wait for that one run. After a success, get returns the value without suspending.

Compared with lazy

  • The initializer is a suspend function, and get is a suspend function.

  • A failure is not stored. The caller gets the exception, and the next get runs the initializer again.

Threading

All members are safe to call from any coroutine and any thread. isInitialized and valueOrNull never suspend.

Properties

Link copied to clipboard

true after the initializer succeeded.

Functions

Link copied to clipboard
suspend fun get(): T

Returns the value. The first call runs the initializer, in the coroutine context of that caller.

Link copied to clipboard
fun valueOrNull(): T?

Returns the value if the initializer already succeeded, else null. It never starts the initializer.