手写封装promise

34 阅读1分钟
const fs = require('fs');

function readFileAsync(path, options) {
  return new Promise((resolve, reject) => {
    fs.readFile(path, options, (err, data) => {
      if (err) {
        reject(err);
      } else {
        resolve(data);
      }
    });
  });
}

// 使用示例
readFileAsync('example.txt', 'utf8')
  .then(data => console.log(data))
  .catch(err => console.error(err));

示例:

const fs = require('fs')
const path = require('path')
const textPath = path.join(__dirname, '/test.md')

// 读取示例文件
fs.readFile(textPath, 'utf8', (err, contrast) => {
  // 通过promisefy转化为链式调用
  const readFileSync = promisefy(fs.readFile)

  readFileSync(textPath, 'utf8')
    .then((res) => {
      console.log(res === contrast) // 此处结果预期:true,即promise返回内容与前面读取内容一致
    })
    .catch((err) => {})
})

const promisefy = (fn) => {
  // TODO 此处完成该函数的封装
 return function(...params){
  return new Promise((resolve,reject)=>{
    fn(...params,(err,data)=>{
      if(err) reject(err)
      else resolve(data)
    })
  })
 }
}

module.exports = promisefy // 请勿删除该行代码