• Automatically calls the given function whenever the given predicate evaluates to true.

    Parameters

    • predicate: (() => boolean)

      The predicate that should be evaluated.

        • (): boolean
        • Returns boolean

    • fn: (() => void)

      The function to be called when the predicate evaluates to true.

        • (): void
        • Returns void

    • Optional options: {
          delay: number;
          signal: AbortSignal;
          timeout: number;
      }

      Optional options.

      • delay: number

        The number of milliseconds to wait before calling the function.

      • signal: AbortSignal

        An AbortSignal to cancel the function call.

      • timeout: number

        The number of milliseconds to wait before timing out.

    Returns (() => void)

    A function that can be called to unsubscribe the callback function from changes in the tracked dependencies.

      • (): void
      • Returns void

    Example

    const a = atom(0, { name: "a" });
    let foo = 0;

    when(
    () => a.value > 3,
    () => {
    foo++;
    }
    );

    a.value = 2; // foo = 0
    a.value = 3; // foo = 0
    a.value = 4; // foo = 1
    a.value = 5; // foo = 1
    a.value = 3; // foo = 1
    a.value = 4; // foo = 2
    a.value = 5; // foo = 2