The type parameter. The actual stored type is Awaited<T>.
Readonly[toThe well-known symbol Symbol.toStringTag used by Object.prototype.toString().
Returns 'OnceAsync' so that Object.prototype.toString.call(once) produces '[object OnceAsync]'.
Gets the contents, initializing it with async fn if empty.
If multiple calls occur concurrently, only the first one will run the
initialization function. Other calls will wait for it to complete.
If that in-flight attempt fails (e.g. a getOrTryInit returning
Err), a waiting getOrInit caller whose own fn has not run yet
retries with its own fn — matching Rust's get_or_try_init.
Not reentrant: fn must not call getOrInit/getOrTryInit on this
same OnceAsync and await the result — the outer initializer waits on
the inner, which in turn waits on the outer, so neither promise ever
settles (a circular wait). This mirrors Rust, where a OnceLock
initializer is likewise not reentrant. A re-entrant call that does not
await the result instead joins the in-flight attempt and resolves with
its value.
A promise that resolves to the stored value (Awaited<T>).
Gets the contents, initializing it with async fn if empty.
If fn returns Err, remains uninitialized.
If multiple calls occur concurrently, only the first one will run the
initialization function. Other calls will wait for it to complete;
if that attempt fails, a waiting caller whose own fn has not run yet
retries with its own fn.
Not reentrant: as with getOrInit, fn must not call
getOrInit/getOrTryInit on this same OnceAsync and await the
result, which would wait on itself and never settle.
The error type.
A promise that resolves to Ok(value) or Err(error).
Sets the contents to value.
If an initialization attempt (getOrInit/getOrTryInit) is currently
in progress, the attempt wins: set() returns Err(value) without
writing, matching Rust's OnceLock::set racing an in-flight
get_or_init.
The value to store.
Ok(undefined) if empty, Err(value) if already initialized or being initialized.
Sets the contents to value if empty, returning a reference to the value.
Unlike set(), this method returns the stored value on success,
and returns both the current and passed values on failure.
If an initialization attempt (getOrInit/getOrTryInit) is currently
in progress, the attempt wins and Err is returned; because the current
value has not been committed yet, it is reported as undefined.
The value to store.
Ok(value) if empty, Err([currentValue, passedValue]) if already initialized or being initialized.
Waits for the cell to be initialized, then returns the value.
If the cell is already initialized, returns immediately. If initialization is in progress, waits for it to complete. If the cell is uninitialized and no initialization is in progress, the returned promise will resolve when another caller initializes the cell.
Unlike an in-flight init attempt, wait() never rejects: a failed
getOrTryInit leaves the cell empty and the waiter keeps waiting until
a later set()/init attempt succeeds. This matches Rust's
OnceLock::wait, which only returns once the cell is initialized.
A promise that resolves to the stored value once initialized.
An async-first container which can be written to only once.
OnceAsync<T>storesAwaited<T>, matching JavaScript's Promise flattening behavior. This meansOnceAsync<Promise<number>>andOnceAsync<number>behave identically, both storingnumber.Key difference from
Once<T>:Once<T>with sync methods storesTas-is (including Promise values)OnceAsync<T>always storesAwaited<T>(flattened value)Since
1.8.0
See
Once for sync-only one-time initialization
Example
Example