Foreword
Node Thanks to its asynchronous non-blocking I / O exhibited outstanding performance and develop. However, asynchronous I / O has also brought a corresponding series of questions, the callback hell, recursively nested. . . Been criticized herein that some asynchronous recording solution that enables Node parallel I / O processing better use
solution
The main solutions are summarized as follows:
- Publish / subscribe model
- Promise / Deferred mode
- Event Control library
Published subscription model
Publish-Subscribe pattern is a design pattern in JavaScript can be used in the form of callback, it will change with the change in part been decoupled, only care about the specific business processes, without the need for intermediate care treatment. Javascript publish subscription model , Node in events
module implements the publish subscribe native mode
const events = require("events");
const fs = require("fs");
const emitter = new events.EventEmitter();
emitter.on("read_foo",function(err,data){
console.log(data);
})
fs.readFile("./profile.jpg",function(err,data){
return emitter.emit("read_foo",err,data);
})
The same event exceed 10 listeners can cause memory leaks, will get a warning, in addition, EventEmitter order to handle error events will detect whether there is error event listeners when an error occurs, if there is an error will be treated to the listener. Can easily inherit EventEmitter by util module
const events = require("events");
const util = require("util");
function Events(){
return events.EventEmitter.call(this);
}
util.inherits(Events,events.EventEmitter);
Partial function
Partial function is determined by the number of times to perform the functions of determining whether to perform the functions of the function
const after = function(times,callback){
let result = {},count = 0;
return function(key,value){
result[key] = value;
if(++count===times){
return callback(result);
}
}
}
According to this property may be utilized asynchronous I / O processing performance of partial functions using Node, a look no asynchronous process using partial functions (common callback Hell process)
fs.readdir(path.join(__dirname,".."),function(err,files){
files.forEach(function(filename,index){
fs.readFile(filename,"utf-8",function(err,file){
// TODO
})
})
})
Node in common in the above code, the common code to complete the task, using the above code is no advantage to the Node asynchronous I / O, it becomes a serial process, but the advantage is that Node parallel processing procedure as the processing
fs.readdir(path.join(__dirname,".."),function(err,files){})
fs.readFile("./profile.jpg","utf-8",function(err,data){})
fs.readFile("./sun.jpg","utf-8",function(err,data){})
Add partial function processing
const fs = require("fs");
const path = require("path");
const events = require("events");
const emitter = new events.EventEmitter();
const after = function(times,callback){
let result = {},count = 0;
return function(key,value){
result[key] = value;
if(++count===times){
return callback(result);
}
}
}
const done = after(3,function(res){
console.log(res)
})
emitter.on("done",done);
fs.readdir(path.join(__dirname,".."),function(err,files){
emitter.emit("done","data1",files);
})
fs.readFile("./profile.jpg","utf-8",function(err,data){
emitter.emit("done","data2",data);
})
fs.readFile("./sun.jpg","utf-8",function(err,data){
emitter.emit("done","data3",data);
})