基于promise.js實(shí)現(xiàn)nodejs的promises庫
更新時間:2014年07月06日 11:19:44 投稿:hebedich
promise是JavaScript實(shí)現(xiàn)優(yōu)雅編程的一個非常不錯的輕量級框架。該框架可以讓你從雜亂的多重異步回調(diào)代碼中解脫出來,并把精力集中到你的業(yè)務(wù)邏輯上。
今天從GIT源碼庫中下載了promise.js,發(fā)現(xiàn)該源碼是基于Web前端JavaScript寫的,并不能直接用于nodejs。還好代碼不是很多,也不是很復(fù)雜。經(jīng)過分析整合,將其實(shí)現(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中簡單實(shí)現(xiàn)Javascript Promise機(jī)制的實(shí)例
- node.js中使用q.js實(shí)現(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對象的使用方法實(shí)例分析
相關(guān)文章
NodeJS學(xué)習(xí)筆記之Connect中間件模塊(二)
本文續(xù)上文的內(nèi)容,介紹下nodejs中connect中間件的使用方式及用途,希望大家喜歡。2015-01-01
Nodejs使用fs-extra模塊進(jìn)行目錄和文件操作用法示例
fs-extra模塊是基于fs?的文件操作相關(guān)工具庫,封裝了一些fs實(shí)現(xiàn)起來相對復(fù)雜的工具,下面這篇文章主要給大家介紹了關(guān)于Nodejs使用fs-extra模塊進(jìn)行目錄和文件操作用法的相關(guān)資料,需要的朋友可以參考下2024-06-06
Node.js配合node-http-proxy解決本地開發(fā)ajax跨域問題
這篇文章主要介紹了Node.js配合node-http-proxy解決本地開發(fā)ajax跨域問題,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-08-08

