ES6-promise封装读取文件

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<script>
// 1.引入fs模块
const fs = require('fs');

// 2.调用方法读取文件
// fs.readFile(文件路径,(err,data)=>{
// // 如果失败 则抛出错误
// if(err) throw err;
// // 如果没错,则输入内容
// console.log(data.toString());
// });

// 3.promise进行封装
const p = new Promise(function(resolve,reject){
fs.readFile(文件路径,(err,data)=>{
// 如果失败
if(err) reject(err);
// 如果成功
resolve(data);
});
});

p.then(function(value){
console.log(value.toString())
},function(reason){
console.log('读取失败')
});
</script>
</body>
</html>

原文地址:https://www.cnblogs.com/weixin2623670713/p/13556751.html