上一篇文章只是简单的对promise原理有个简单的实现,这次增加了
all
方法,同时调用多次异步请求- 对
resolve
和reject
方法进行了重构,增加了对多个异步调用的处理 - 对
promse
进行了改造,除掉了事件触发机制,增加了异步队列
详细代码如下:
/**
* Created with JetBrains WebStorm.
* User: xuwenmin
* Date: 14-4-26
* Time: 下午1:01
* To change this template use File | Settings | File Templates.
*/
var EventEmitter = require('events').EventEmitter;
var http = require('http');
var util = require('util');
// 定义promise对象
var Promise = function(){
this.queue = [];
this.isPromise = true;
}
// then 方法为promise/A 规范中的方法
Promise.prototype.then = function(successHandler, errorHandler, progressHandler){
var hanlder = {};
if (typeof successHandler == 'function'){
hanlder.fulfilled = successHandler;
}
if (typeof errorHandler === 'function'){
hanlder.error = errorHandler;
}
this.queue.push(hanlder);
return this;
}
// 定义延迟对象
// 包含一个状态和一个promise对象
var Deferred = function(){
this.promise = new Promise();
}
// 同时处理多个异步的情况
Deferred.prototype.all = function(promises){
var count = promises.length;
var that = this;
var results = [];
promises.forEach(function(promise, i){
promise.then(function(data){
count--;
results[i] = data;
// 如果执行玩了,则调用完成方法
if(count == 0){
that.resolve(results);
}
}, function(err){
that.reject(err);
});
});
return this.promise;
}
// 生成回调函数
Deferred.prototype.callback = function(){
var that = this;
return function(err, data){
if(err){
that.reject(err);
}else{
that.resolve(data);
}
};
}
Deferred.prototype.resolve = function(obj){
var promise = this.promise;
var handler;
while((handler = promise.queue.shift())){
if (handler && handler.fulfilled){
var ret = handler.fulfilled(obj);
if(ret && ret.isPromise){
ret.queue = promise.queue;
this.promise = ret;
return;
}
}
}
}
Deferred.prototype.reject = function(err){
var promise = this.promise;
var handler;
while((handler = promise.queue.shift())){
if (handler && handler.error){
var ret = handler.error(err);
if(ret && ret.isPromise){
ret.queue = promise.queue;
this.promise = ret;
return;
}
}
}
}
var client1 = function(){
var options = {
hostname:'www.baidu.com',
port:80,
path:'/',
method: 'get'
};
var deferred = new Deferred();
var req = http.request(options, function(res){
res.setEncoding('utf-8');
var data = '';
res.on('data', function(chunk){
data += chunk;
});
res.on('end', function(){
deferred.resolve(data);
});
});
req.on('error', function(err){
deferred.reject(err);
})
req.end();
return deferred.promise;
}
var client2 = function(predata){
var options = {
hostname:'www.hao123.com',
port:80,
path:'/',
method: 'get'
};
var deferred = new Deferred();
var req = http.request(options, function(res){
res.setEncoding('utf-8');
var data = '';
res.on('data', function(chunk){
data += chunk;
});
res.on('end', function(){
data += predata;
deferred.resolve(data);
});
});
req.on('error', function(err){
deferred.reject(err);
})
req.end();
return deferred.promise;
}
//
client1().then(function(msg){
return client2(msg);
}).then(function(data){
console.log('\033[96m 两次请求的总数据:\033[39m',data);
});
总结
上面的代码参考了深入浅出NodeJS
,权当心得体会.
时间: 2025-01-30 09:12:07