node核心模块promise化

  1. 先安装 bluebird
npm install bluebird
  1. 使用promisifyAll函数,可以将模块导出的接口promise化,注意处理后的API函数名加Async如:fs.readFileAsync()
var Promise = require('bluebird')
const fs = Promise.promisifyAll(require('fs'))
fs.readFileAsync('./1.j').then(value => {
  console.log(value.toString());
}).catch(err => {
  console.log(err);
})
fs.writeFileAsync('驿外.txt', '驿外断桥边').then(value => {
  console.log(value);
})
posted @ 2021-05-16 14:01  丁同亚的博客  阅读(70)  评论(0编辑  收藏  举报