SOURCE

// node 很多api 的回调都是 err first类型 的函数,比如 readFile(path, options, (err, res) => {})
// 希望改成基于promise的使用方式, readFilePromisify(path, options).then(res => {})
// 请实现一个 promisify方法 

function readFile(path, options, callback) {
	console.log('path', path, 'options', options);
    callback(null, 'xxxx')
}

function promisify(fn) {
  // code here
    return (...res) => {
        return new Promise((resolve, rej) => {
            fn(...res, (err, data) => {
                if(err){
                    return rej(err)
                }
                resolve(data)
            })
        })
    }
}

const promisifiedReadFile = promisify(readFile);

promisifiedReadFile('path', 'options').then(data => {
    // data xxxx
	console.log('data', data);
}).catch(e => {
	console.log(e)
})
console 命令行工具 X clear

                    
>
console