node + promise 实现文件读写
const fs = require('fs');
const promise = new Promise((resolve, reject) => {
fs.open('./c.txt', 'w', (err, fd) => {
if (!err) {
resolve(fd);
}else {
reject(err);
}
})
});
promise.then((fd) => {
return new Promise((resolve, reject) => {
fs.write(fd, 'wswsws', (err, result) => {
if(err){
console.log(err);
}
resolved(fd);
})
})
}).then((fd) => {
return new Promise((resolve, reject) => {
fs.close(fd,(err) => {
if (!err) {
resolve();
}else {
reject(err);
}
})
})
}).then(() => {
console.log('success')
}).catch((err) => {
console.log(err);
})