2000字范文,分享全网优秀范文,学习好帮手!
2000字范文 > 基于promise.js实现nodejs的promises库【node.js】

基于promise.js实现nodejs的promises库【node.js】

时间:2020-05-24 17:17:27

相关推荐

基于promise.js实现nodejs的promises库【node.js】

web前端|js教程

nodejs,promises

web前端-js教程

今天从GIT源码库中下载了promise.js,发现该源码是基于Web前端JavaScript写的,并不能直接用于nodejs。还好代码不是很多,也不是很复杂。经过分析整合,将其实现为nodejs的一个框架,代码如下:

j2ee应用开发源码,vscode 隐藏缩略图,ubuntu无效主题,tomcat默认部署配置,爬虫任务图谱,php变长参数,全网seo推广有用吗,小说合作网站源,微赞如何安装pc模板lzw

(function(){/*** Copyright - (c) Pierre Duquesne * script: promise.js* description: promises的nodejs模块* modified: /stackp/promisejs* authors: alwu007@* */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<this._callbacks.length; i++) { this._callbacks[i].apply(null, arguments); } this._callbacks = [];};Promise.join = function(promises){ var p = new Promise(); var results = []; if (!promises || !promises.length) { p.done(results); return p; } var numdone = 0; var total = promises.length; function notifier(i) { return function() {numdone += 1;results[i] = Array.prototype.slice.call(arguments);if (numdone === total) { p.done(results);} }; } for (var i = 0; i < total; i++) { promises[i].then(notifier(i)); } return p;};Promise.chain = function(funcs, args) { var p = new Promise(); if (!funcs || !funcs.length) { p.done.apply(p, args); } else { funcs[0].apply(null, args).then(function(){funcs.splice(0, 1);Promise.chain(funcs, arguments).then(function(){ p.done.apply(p, arguments);}); }); } return p;};})();

另附测试代码如下:

彩虹秒赞已源码授权,zynq安装ubuntu,tomcat教程与调优,爬虫外包价格,php服务器端开发教程,seo和sem的全称是什么lzw

/*** 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 oy; }); }).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 oy; }); }).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, oy); }, 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);});

调查问卷网站源码,vscode 使用啥编写,ubuntu中boot,tomcat uri编码,sqlite 嵌入式使用,市场三大主流前端框架分别是,用爬虫做猜单词游戏,简单php分页,韶关seo优化渠道, 网站所有文件编译dll,sketch 网页模板,wordpress自定义文章页面模板lzw

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。