一、Demo
1、首先定义 module 文件:bbb.js
const fs = require("fs");
function readFileSync() {
let result = fs.readFileSync("./result.log");
return result;
}
async function readFileAsync() {
let result = await new Promise((resolve, reject) => {
fs.readFile("./result.log", (err, data) => {
if (err) reject(err);
resolve(data);
});
});
return result;
}
module.exports = { readFileSync, readFileAsync };
2、添加 main 文件:aaa.js
const bbb = require("./bbb");
// readFileSync
// console.log("re:", bbb.readFileSync());
// readFileAsync
(async () => {
let result;
result = await bbb.readFileAsync();
console.log("re:", result);
})();
二、追问
在 main 文件中还是需要有
(async () => {})();
来包裹整个 function content,有没有更简洁的写法呢?
原文地址:https://www.cnblogs.com/xjnotxj/p/11113502.html
时间: 2024-10-24 07:59:40