@pengzhanbo/utils - v3.7.3
    Preparing search index...

    Function withTimeout

    • Executes an async function and enforces a timeout.

      执行异步函数, 超时则强制拒绝。支持 AbortController 取消和超时回退值。

      Type Parameters

      • T

      Parameters

      • run: (signal: AbortSignal) => Promise<T>

        The async function to execute, receives an AbortSignal for cancellation awareness. When an external signal is provided via options, it is merged with the internal signal using AbortSignal.any(), so the function can respond to both external cancellation and internal timeout/cancel. 执行的异步函数,接收 AbortSignal 用于感知取消。若通过 options 提供了外部 signal, 则使用 AbortSignal.any() 与内部信号合并,使函数能同时响应外部取消和内部超时/取消。

      • ms: number

        The number of milliseconds before timing out. 超时毫秒数

      • Optionaloptions: WithTimeoutOptions<T>

        Options. 选项

        • Optionalfallback?: T

          Fallback value returned on timeout instead of throwing (optional) / 超时时返回的回退值(可选)

        • Optionalmessage?: string

          Custom error message for timeout / 超时错误信息

        • Optionalsignal?: AbortSignal

          External AbortSignal for cancellation awareness in run(). / 外部取消信号(通过 AbortSignal.any() 合并后传给 run 函数)

      Returns WithTimeoutResult<T>

      A promise that resolves with the result, or rejects with TimeoutError. Supports .cancel() method for manual cancellation.

      一个 promise,解析为异步函数结果,或超时/取消时拒绝。 支持 .cancel() 方法手动取消。

      Basic usage:

      const data = await withTimeout(
      async (signal) => {
      const res = await fetch(url, { signal })
      return res.json()
      },
      5000,
      )

      Combined signal with external AbortController:

      const controller = new AbortController()
      const result = await withTimeout(
      async (signal) => {
      // signal is a combined signal from AbortSignal.any()
      // It responds to both external abort and internal timeout/cancel
      return fetch(url, { signal }).then(r => r.json())
      },
      5000,
      { signal: controller.signal },
      )
      // Externally aborting cancels fetch AND withTimeout:
      controller.abort()

      With fallback value:

      const result = await withTimeout(
      () => slowOperation(),
      1000,
      { fallback: 'default' },
      ) // => 'default' if timed out

      Manual cancellation:

      const task = withTimeout(() => longRunning(), 10000)
      task.cancel() // Cancel immediately without waiting
    • Executes an async function and enforces a timeout.

      执行异步函数, 超时则强制拒绝。支持 AbortController 取消和超时回退值。

      Type Parameters

      • T

      Parameters

      • run: () => Promise<T>

        The async function to execute, receives an AbortSignal for cancellation awareness. When an external signal is provided via options, it is merged with the internal signal using AbortSignal.any(), so the function can respond to both external cancellation and internal timeout/cancel. 执行的异步函数,接收 AbortSignal 用于感知取消。若通过 options 提供了外部 signal, 则使用 AbortSignal.any() 与内部信号合并,使函数能同时响应外部取消和内部超时/取消。

      • ms: number

        The number of milliseconds before timing out. 超时毫秒数

      • Optionaloptions: WithTimeoutOptions<T>

        Options. 选项

        • Optionalfallback?: T

          Fallback value returned on timeout instead of throwing (optional) / 超时时返回的回退值(可选)

        • Optionalmessage?: string

          Custom error message for timeout / 超时错误信息

        • Optionalsignal?: AbortSignal

          External AbortSignal for cancellation awareness in run(). / 外部取消信号(通过 AbortSignal.any() 合并后传给 run 函数)

      Returns WithTimeoutResult<T>

      A promise that resolves with the result, or rejects with TimeoutError. Supports .cancel() method for manual cancellation.

      一个 promise,解析为异步函数结果,或超时/取消时拒绝。 支持 .cancel() 方法手动取消。

      Basic usage:

      const data = await withTimeout(
      async (signal) => {
      const res = await fetch(url, { signal })
      return res.json()
      },
      5000,
      )

      Combined signal with external AbortController:

      const controller = new AbortController()
      const result = await withTimeout(
      async (signal) => {
      // signal is a combined signal from AbortSignal.any()
      // It responds to both external abort and internal timeout/cancel
      return fetch(url, { signal }).then(r => r.json())
      },
      5000,
      { signal: controller.signal },
      )
      // Externally aborting cancels fetch AND withTimeout:
      controller.abort()

      With fallback value:

      const result = await withTimeout(
      () => slowOperation(),
      1000,
      { fallback: 'default' },
      ) // => 'default' if timed out

      Manual cancellation:

      const task = withTimeout(() => longRunning(), 10000)
      task.cancel() // Cancel immediately without waiting