function promisify(fn) {
}
function readFile(path, cb) {
setTimeout(() => {
if (Math.random() > 0.5) {
cb(new Error(`读取${path}出错`));
} else {
cb(null, `读取${path}成功`);
}
}, 100);
}
readFile('file.text', (err, value) => {
if (err) {
console.log('error ', err);
return;
}
console.log('success ', value);
});
const readFilePromise = promisify(readFile);
readFilePromise('promisefile.txt')
.then((res) => {
console.log('promise success', res);
})
.catch((error) => {
console.log('promise error', error);
});
console