@supuwoerc/toolkit
    Preparing search index...

    Function promisify

    • 将基于回调的函数转换为返回 Promise 的函数 Converts a callback-based function to a Promise-returning function

      Type Parameters

      • T

        Promise 解析值的类型 The type of the resolved Promise value

      Parameters

      • fn: (...args: any[]) => void

        需要转换的原始回调函数 The original callback function to be converted

      • Optionalcontext: any

        函数执行时的上下文(this 值) The context (this value) for function execution

      Returns (...args: any[]) => Promise<T>

      返回一个新函数,该函数接受与原函数相同的参数,但返回 Promise Returns a new function that accepts the same arguments as the original but returns a Promise

      // 示例:转换 Node.js 的 fs.readFile
      // Example: Convert Node.js fs.readFile
      const readFilePromise = promisify(fs.readFile, fs);
      readFilePromise('file.txt', 'utf8').then(console.log);