@supuwoerc/toolkit
    Preparing search index...

    Function withTimeout

    • Wraps a promise with a timeout 为 Promise 添加超时包装

      Type Parameters

      • T

      Parameters

      • promise: Promise<T>

        The promise to wrap with timeout 要添加超时的 Promise

      • timeoutMs: number

        Timeout duration in milliseconds 超时时间(毫秒)

      • errorMessage: string | Error = 'Operation timeout'

        Error message or Error object for timeout (default: 'Operation timeout') 超时时使用的错误消息或 Error 对象(默认:'Operation timeout')

      Returns Promise<T>

      A promise that either resolves/rejects with the original promise or rejects on timeout 返回一个 Promise,要么随原始 Promise 完成/拒绝,要么在超时时拒绝

      // Basic usage
      // 基本用法
      const result = await withTimeout(fetchData(), 5000, 'Request timeout')
      // With custom Error object
      // 使用自定义 Error 对象
      const timeoutError = new Error('Custom timeout')
      const result = await withTimeout(longOperation(), 3000, timeoutError)