基于promise.js實現(xiàn)nodejs的promises庫
今天從GIT源碼庫中下載了promise.js,發(fā)現(xiàn)該源碼是基于Web前端JavaScript寫的,并不能直接用于nodejs。還好代碼不是很多,也不是很復(fù)雜。經(jīng)過分析整合,將其實現(xiàn)為nodejs的一個框架,代碼如下:
(function(){ /** * Copyright 2012-2013 (c) Pierre Duquesne <stackp@online.fr> * 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){ //處理回調(diào)結(jié)果的方法 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; }; })();
另附測試代碼如下:
/** * 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); });
- node使用promise替代回調(diào)函數(shù)
- async/await與promise(nodejs中的異步操作問題)
- NodeJS的Promise的用法解析
- NodeJS中利用Promise來封裝異步函數(shù)
- nodejs中簡單實現(xiàn)Javascript Promise機制的實例
- node.js中使用q.js實現(xiàn)api的promise化
- Nodejs學(xué)習(xí)筆記之Global Objects全局對象
- 用nodejs訪問ActiveX對象,以操作Access數(shù)據(jù)庫為例。
- 詳解nodeJS之二進(jìn)制buffer對象
- Node.js 基礎(chǔ)教程之全局對象
- 深入理解Node內(nèi)建模塊和對象
- node.js Promise對象的使用方法實例分析
相關(guān)文章
nodejs 中模擬實現(xiàn) emmiter 自定義事件
這篇文章主要介紹了Nodejs中自定義事件實例,比較簡單的一個例子,需要的朋友可以參考下。2016-02-02iOS + node.js使用Socket.IO框架進(jìn)行實時通信示例
本篇文章主要介紹了iOS + node.js使用Socket.IO框架進(jìn)行實時通信示例,具有一定的參考價值,感興趣的小伙伴們可以參考一下。2017-04-04npm install --save 、--save-dev 、-D、-S&nb
這篇文章主要介紹了npm install --save 、--save-dev 、-D、-S 的區(qū)別與NODE_ENV的配置方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-08-08node.js基于dgram數(shù)據(jù)報模塊創(chuàng)建UDP服務(wù)器和客戶端操作示例
這篇文章主要介紹了node.js基于dgram數(shù)據(jù)報模塊創(chuàng)建UDP服務(wù)器和客戶端操作,結(jié)合實例形式分析了node.js使用dgram數(shù)據(jù)報模塊創(chuàng)建UDP服務(wù)器和客戶端,以及進(jìn)行UDP廣播、組播相關(guān)操作技巧,需要的朋友可以參考下2020-02-02Node.js發(fā)送HTTP客戶端請求并顯示響應(yīng)結(jié)果的方法示例
這篇文章主要介紹了Node.js發(fā)送HTTP客戶端請求并顯示響應(yīng)結(jié)果的方法,結(jié)合完整實例形式分析了nodejs發(fā)送http請求及響應(yīng)的相關(guān)操作技巧,需要的朋友可以參考下2017-04-04NodeJS學(xué)習(xí)筆記之網(wǎng)絡(luò)編程
Node.js采用了Google Chrome瀏覽器的V8引擎,性能很好,同時還提供了很多系統(tǒng)級的API,如文件操作、網(wǎng)絡(luò)編程等。Node.js則是一個全面的后臺運行時,為Javascript提供了其他語言能夠?qū)崿F(xiàn)的許多功能。今天我們來看下Nodejs的網(wǎng)絡(luò)編程2014-08-08node.js中的buffer.Buffer.isEncoding方法使用說明
這篇文章主要介紹了node.js中的buffer.Buffer.isEncoding方法使用說明,本文介紹了buffer.Buffer.isEncoding的方法說明、語法、接收參數(shù)、使用實例和實現(xiàn)源碼,需要的朋友可以參考下2014-12-12