浅谈Node异步编程的机制(2)
偏函数方案
var after = function(times,callback){
var count = 0, result = {};
return function(key,value){
results[key] = value;
count++;
if(count === times){
callback(results);
}
}
}
var done = after(times,render);
var emitter = new events.Emitter();
emitter.on('done',done); //一个侦听器
emitter.on('done',other); //如果业务增长,可以完成多对多的方案
fs.readFile(template_path,"utf8",function(err,template){
emitter.emit('done','template',template);
})
db.query(sql,function(err,data){
emitter.emit('done','data',data);
})
l10n.get(function(err,resources){
emitter.emit('done','resources',resources)
})
引入EventProxy模块方案
var proxy = new EventProxy();
proxy.all('template','data','resources',function(template,data,resources){
//TODO
})
fs.readFile(template_path,'utf8',function(err,template){
proxy.emit('template',template);
})
db.query(sql,function(err,data){
proxy.emit('data',data);
})
l10n.get(function(err,resources){
proxy.emit('resources',resources);
})
Promise/Deferred模式
以上使用事件的方式时,执行流程都需要被预先设定,这是发布/订阅模式的运行机制所决定的。
$.get('/api',{
success:onSuccess,
err:onError,
complete:onComplete
})
//需要严谨设置目标
那么是否有一种先执行异步调用,延迟传递处理的方式的?接下来要说的就是针对这种情况的方式:Promise/Deferred模式
Promise/A
Promise/A提议对单个异步操作做出了这样的抽象定义:
- Promise操作只会处在三种状态的一种:未完成态,完成态和失败态。
- Promise的状态只会出现从未完成态向完成态或失败态转化,不能逆反,完成态和失败态不能相互转化
- Promise的状态一旦转化,就不能被更改。
一个Promise对象只要具备then()即可
- 接受完成态、错误态的回调方法
- 可选地支持progress事件回调作为第三个方法
- then()方法只接受function对象,其余对象将被忽略
- then()方法继续返回Promise对象,以实现链式调用
通过Node的events模块来模拟一个Promise的实现
var Promise = function(){
EventEmitter.call(this)
}
util.inherits(Promise,EventEmitter);
Promise.prototype.then = function(fulfilledHandler,errHandler,progeressHandler){
if(typeof fulfilledHandler === 'function'){
this.once('success',fulfilledHandler); //实现监听对应事件
}
if(typeof errorHandler === 'function'){
this.once('error',errorHandler)
}
if(typeof progressHandler === 'function'){
this.on('progress',progressHandler);
}
return this;
}
内容版权声明:除非注明,否则皆为本站原创文章。
