node模块包装为Promise书写法
1. const Promise = require('bluebird')
const fs = Promise.promisifyAll(Promise.promisify(require('fs')))
2.针对node npm部分模块,比如xml2js,不支持bluebird包装的形式,解决办法
const xml2js = require('xml2js')
const Parser = new xml2js.Parser()
const parseStringAsync = xml => {
return new Promise((resolve, reject) => {
Parser.parseString(xml, (err, result) => {
if (err) {
reject(err)
} else {
resolve(result)
}
})
})
}