回调函数处理异步
const mime = require("./model/mime");
function getMime(cb) {
fs.readFile("./json/mime.json", (err, data) => {
if (err) throw err;
cb(JSON.parse(data.toString()));
});
}
getMime(val => {
console.log(val);
});
events 模块处理异步
const events = require("events");
const EventEmitter = new events.EventEmitter();
EventEmitter.on("to_parent", data => {
console.log("接收到了这个广播事件");
console.log(data);
});
setTimeout(() => {
console.log("开始广播");
EventEmitter.emit("to_parent", "发送的数据");
}, 2000);
exports.getType = (fs, EventEmitter, extname) => {
fs.readFile("./json/mime.json", (err, data) => {
if (err) throw err;
mimeType = JSON.parse(data.toString())[extname];
EventEmitter.emit("mime_type", mimeType);
});
};
网友评论