您好,欢迎访问一九零五行业门户网

基于promise.js实现nodejs的promises库_node.js

今天从git源码库中下载了promise.js,发现该源码是基于web前端javascript写的,并不能直接用于nodejs。还好代码不是很多,也不是很复杂。经过分析整合,将其实现为nodejs的一个框架,代码如下:
(function(){/*** copyright 2012-2013 (c) pierre duquesne * script: promise.js* description: promises的nodejs模块* modified: https://github.com/stackp/promisejs* authors: alwu007@sina.cn* */var promise = exports.promise = function(){ this._callbacks = [];};promise.prototype.then = function(func, context){ //处理回调结果的方法 function docallbackresults(r) { if (r instanceof promise) { r.then(function(err, values){ p.done(err, values); }); } else { p.done(null, r); } } var p = new promise(); if (this._isdone) { var results = func.apply(context, this.results); docallbackresults(results); } else { this._callbacks.push(function(){ var results = func.apply(context, arguments); docallbackresults(results); }); } return p;};promise.prototype.done = function(){ this.results = arguments; this._isdone = true; for (var i=0; i 另附测试代码如下:
/*** script: test.js* description: promise.js测试代码* */var promise = require('./mypromise');function asyncfoo() { var p = new promise.promise(); settimeout(function(){ p.done(); }, 1000); return p;}function syncfoo() { var p = new promise.promise(); p.done(); return p;}var o = {};/*asyncfoo().then(function(){ return 'raymond';}, o).then(function(err, name){ o.name = name; return asyncfoo().then(asyncfoo).then(function(){ return asyncfoo().then(asyncfoo).then(function(){ return 18; }); });}, o).then(function(err, age){ o.age = age; return asyncfoo().then(asyncfoo).then(function(){ return asyncfoo().then(asyncfoo).then(function(){ return 'boy'; }); }).then(function(err, sex){ return sex; });}).then(function(err, sex){ o.sex = sex; return 'hello, world!';}).then(function(err, say){ o.say = say; console.dir(o);});syncfoo().then(function(){ return 'raymond';}, o).then(function(err, name){ o.name = name; return syncfoo().then(syncfoo).then(function(){ return syncfoo().then(syncfoo).then(function(){ return 18; }); });}, o).then(function(err, age){ o.age = age; return asyncfoo().then(asyncfoo).then(function(){ return asyncfoo().then(asyncfoo).then(function(){ return 'boy'; }); }).then(function(err, sex){ return sex; });}).then(function(err, sex){ o.sex = sex; return 'hello, world!';}).then(function(err, say){ o.say = say; console.dir(o);});*/function asyncfoo1(){ var p = new promise.promise(); settimeout(function(){ p.done(null, 'raymond'); }, 1000); return p;}function asyncfoo2(err, name){ o.name = name; var p = new promise.promise(); settimeout(function(){ p.done(null, 18); }, 1000); return p;}function asyncfoo3(err, age){ o.age = age; var p = new promise.promise(); settimeout(function(){ p.done(null, 'boy'); }, 1000); return p;}function asyncfoo4(){ var p = new promise.promise(); settimeout(function(){ p.done(null, 'hello, world!'); }, 1000); return p;}promise.promise.chain([asyncfoo1, asyncfoo2, asyncfoo3]).then(function(err, sex){ o.sex = sex; return asyncfoo4();}).then(function(err, say){ o.say = say;}).then(function(){ console.dir(o);});
其它类似信息

推荐信息