happy-rusty
    Preparing search index...

    Interface LazyAsync<T>

    A value which is initialized asynchronously on the first access.

    The initialization function can return PromiseLike<T> or T. If multiple calls to force() occur concurrently before initialization completes, only one initialization will run.

    1.6.0

    Lazy for sync-only lazy initialization

    const db = LazyAsync(async () => {
    console.log('Connecting...');
    return await Database.connect(url);
    });

    // Multiple concurrent accesses - only one connection
    const [db1, db2] = await Promise.all([
    db.force(),
    db.force(),
    ]);
    // db1 === db2
    interface LazyAsync<T> {
        "[toStringTag]": "LazyAsync";
        force(): Promise<Awaited<T>>;
        get(): Option<Awaited<T>>;
        isInitialized(): boolean;
        toString(): string;
    }

    Type Parameters

    • T

      The type of the value stored.

    Index

    Properties

    "[toStringTag]": "LazyAsync"

    The well-known symbol Symbol.toStringTag used by Object.prototype.toString(). Returns 'LazyAsync' so that Object.prototype.toString.call(lazy) produces '[object LazyAsync]'.

    Methods

    • Forces the evaluation of this lazy value and returns a promise to the result.

      If the value has already been initialized, returns the cached value. If initialization is in progress, waits for it to complete. Otherwise, starts initialization.

      Returns Promise<Awaited<T>>

      A promise that resolves to the initialized value.

      Rejects with any error thrown or rejected by the initialization function.

      const lazy = LazyAsync(async () => {
      await delay(100);
      return 42;
      });
      console.log(await lazy.force()); // 42
    • Gets the value if it has been initialized.

      Unlike force(), this does not trigger initialization.

      Returns Option<Awaited<T>>

      Some(value) if initialized, None otherwise.

      const lazy = LazyAsync(async () => 42);
      console.log(lazy.get()); // None

      await lazy.force();
      console.log(lazy.get()); // Some(42)
    • Returns true if the value has been initialized.

      Note: Returns false while initialization is in progress.

      Returns boolean

      const lazy = LazyAsync(async () => 42);
      console.log(lazy.isInitialized()); // false

      await lazy.force();
      console.log(lazy.isInitialized()); // true
    • Custom toString implementation.

      Returns string

      const lazy = LazyAsync(async () => 42);
      console.log(lazy.toString()); // 'LazyAsync(<uninitialized>)'

      await lazy.force();
      console.log(lazy.toString()); // 'LazyAsync(42)'