@adviser/cement - v0.0.0
    Preparing search index...

    Function timeouted

    • Executes an action with comprehensive timeout and abort handling.

      Wraps a promise or async function with timeout, abort signal, and error handling capabilities. The action can be controlled via AbortController and will properly clean up resources.

      Type Parameters

      • T

        The type of the action's result value

      • CTX = void

        Optional context type passed through to callbacks

      Parameters

      • action: TimeoutAction<T>

        Either a Promise or a function that receives an AbortController and returns a Promise

      • options: Partial<TimeoutActionOptions<CTX>> = {}

        Optional configuration:

        • timeout: Timeout duration in milliseconds (default: 30000). Set to 0 or negative to disable.
        • signal: External AbortSignal to link for cancellation
        • controller: External AbortController to use instead of creating a new one
        • ctx: Context object passed through in the result
        • onTimeout: Callback invoked when timeout occurs
        • onAbort: Callback invoked when aborted (with abort reason)
        • onError: Callback invoked when action throws an error
        • onAbortAction: Callback for cleanup when action needs to be aborted

      Returns Promise<TimeoutResult<T, unknown>>

      Promise resolving to a TimeoutResult containing:

      • state: "success" | "timeout" | "aborted" | "error"
      • value (if success), error (if error), or reason (if aborted)
      • duration: Elapsed time in milliseconds
      • ctx: The context object if provided
      // With a function that can be aborted
      const result = await timeouted(
      async (controller) => {
      return fetch(url, { signal: controller.signal });
      },
      { timeout: 5000 }
      );

      if (isSuccess(result)) {
      console.log('Request completed:', result.value);
      } else if (isTimeout(result)) {
      console.log('Request timed out after', result.duration, 'ms');
      }

      // With a direct promise
      const result2 = await timeouted(
      fetch(url),
      { timeout: 3000 }
      );