@nevware21/ts-utils
    Preparing search index...

    Function scheduleMicrotask

    • Schedules a callback to run in the microtask queue.

      It uses the native queueMicrotask when available, otherwise falls back to Promise.resolve().then(...), and if Promise is unavailable it falls back to scheduleTimeout(..., 0). Unlike standard microtasks, this helper returns a cancellable ITimerHandler so scheduled callbacks can be canceled before execution. This provides consistent microtask scheduling behavior across all supported runtimes, including Node.js, browsers, and web workers.

      Parameters

      • callback: () => void

        The callback to execute.

      • Optionaloptions: MicroTaskOptions

        Optional per-call fallback options when queueMicrotask is unavailable.

      Returns ITimerHandler

      A handler that can be used to cancel or refresh the scheduled callback.

      0.15.0

      let order: string[] = [];
      order.push("sync");

      const handler = scheduleMicrotask(() => {
      order.push("microtask");
      });

      scheduleTimeout(() => {
      order.push("timeout");
      }, 0);

      // order becomes ["sync", "microtask", "timeout"]
      handler.enabled; // true until the microtask executes
      scheduleMicrotask(() => {
      console.log("custom fallback scheduler");
      }, {
      scheduleFn: (cb) => {
      setTimeout(cb, 0);
      }
      });
      // Args only
      scheduleMicrotask((name: string, count: number) => {
      console.log(name, count);
      }, ["task", 1]);

      // Args + options
      scheduleMicrotask((name: string, count: number) => {
      console.log(name, count);
      }, ["task", 2], {
      useTimeout: true
      });
    • Schedules a callback to run in the microtask queue.

      It uses the native queueMicrotask when available, otherwise falls back to Promise.resolve().then(...), and if Promise is unavailable it falls back to scheduleTimeout(..., 0). Unlike standard microtasks, this helper returns a cancellable ITimerHandler so scheduled callbacks can be canceled before execution. This provides consistent microtask scheduling behavior across all supported runtimes, including Node.js, browsers, and web workers.

      Type Parameters

      • TArgs extends any[]

      Parameters

      • callback: (...args: TArgs) => void

        The callback to execute.

      • args: TArgs

        Optional callback arguments to pass when the callback executes.

      • Optionaloptions: MicroTaskOptions

        Optional per-call fallback options when queueMicrotask is unavailable.

      Returns ITimerHandler

      A handler that can be used to cancel or refresh the scheduled callback.

      0.15.0

      let order: string[] = [];
      order.push("sync");

      const handler = scheduleMicrotask(() => {
      order.push("microtask");
      });

      scheduleTimeout(() => {
      order.push("timeout");
      }, 0);

      // order becomes ["sync", "microtask", "timeout"]
      handler.enabled; // true until the microtask executes
      scheduleMicrotask(() => {
      console.log("custom fallback scheduler");
      }, {
      scheduleFn: (cb) => {
      setTimeout(cb, 0);
      }
      });
      // Args only
      scheduleMicrotask((name: string, count: number) => {
      console.log(name, count);
      }, ["task", 1]);

      // Args + options
      scheduleMicrotask((name: string, count: number) => {
      console.log(name, count);
      }, ["task", 2], {
      useTimeout: true
      });