happy-rusty
    Preparing search index...

    Interface FnOnceAsync<A, R>

    An async function wrapper that can only be called once.

    After the first invocation, the function is consumed and cannot be called again. This mirrors Rust's AsyncFnOnce trait (stabilized in Rust 1.85), which represents async closures that take ownership of captured variables and can only be called once.

    Use cases:

    • One-time async callbacks (cleanup functions, initialization)
    • Ensuring certain async operations execute exactly once
    • Async resource disposal patterns

    1.8.0

    FnOnce for sync one-time callable functions

    // Basic usage
    const fetchData = FnOnceAsync(async (id: number) => {
    const response = await fetch(`/api/data/${id}`);
    return response.json();
    });

    const data = await fetchData.call(1);
    // fetchData.call(2); // Throws Error: FnOnceAsync has already been consumed
    // Safe call with tryCall
    const initOnce = FnOnceAsync(async () => {
    await someAsyncSetup();
    return { initialized: true };
    });

    const result1 = await initOnce.tryCall(); // Some({ initialized: true })
    const result2 = await initOnce.tryCall(); // None
    // One-time async resource cleanup
    const cleanup = FnOnceAsync(async () => {
    await closeConnections();
    await flushLogs();
    });

    await cleanup.tryCall(); // Performs cleanup
    await cleanup.tryCall(); // Returns None, no effect
    interface FnOnceAsync<A extends unknown[], R> {
        "[toStringTag]": "FnOnceAsync";
        call(...args: A): Promise<Awaited<R>>;
        isConsumed(): boolean;
        toString(): string;
        tryCall(...args: A): AsyncOption<Awaited<R>>;
    }

    Type Parameters

    • A extends unknown[]

      Tuple type of the function arguments.

    • R

      The resolved type of the Promise returned by the async function.

    Index

    Properties

    "[toStringTag]": "FnOnceAsync"

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

    const fn = FnOnceAsync(async () => 42);
    console.log(Object.prototype.toString.call(fn)); // '[object FnOnceAsync]'

    Methods

    • Calls the async function with the provided arguments, consuming it.

      Parameters

      • ...args: A

        The arguments to pass to the function.

      Returns Promise<Awaited<R>>

      A Promise that resolves to the return value of the function.

      If the function has already been called.

      const fetchUser = FnOnceAsync(async (id: number) => {
      const response = await fetch(`/api/users/${id}`);
      return response.json();
      });

      const user = await fetchUser.call(1);
      // await fetchUser.call(2); // Throws Error
    • Returns true if the function has been consumed (called).

      Returns boolean

      const fn = FnOnceAsync(async () => 'done');
      console.log(fn.isConsumed()); // false
      await fn.call();
      console.log(fn.isConsumed()); // true
    • Custom toString implementation.

      Returns string

      const fn = FnOnceAsync(async () => 42);
      console.log(fn.toString()); // 'FnOnceAsync(pending)' or 'FnOnceAsync(consumed)'
    • Attempts to call the async function, returning Some(result) if successful or None if the function has already been consumed.

      This is the safe alternative to call() that never throws due to consumption. The returned Promise may still reject if the underlying async function throws.

      Parameters

      • ...args: A

        The arguments to pass to the function.

      Returns AsyncOption<Awaited<R>>

      A Promise that resolves to Some(result) if the function was called, None if already consumed.

      const fetchData = FnOnceAsync(async (id: number) => {
      const response = await fetch(`/api/data/${id}`);
      return response.json();
      });

      const result1 = await fetchData.tryCall(1); // Some(data)
      const result2 = await fetchData.tryCall(2); // None