NodeJS Multiple Callback解決之使用Q Promises
來(lái)源:程序員人生 發(fā)布時(shí)間:2014-12-15 09:30:47 閱讀次數(shù):3736次
在上1篇《Javascript Promises模式――相當(dāng)酷的Callback Hell終結(jié)者》我們介紹了Javascript的Promise模式,接著我們就把Javascript Promise用到我們的代碼中。
JavaScript Promise庫(kù) Q
之前試著用過(guò)Q,但是沒(méi)有成功。或說(shuō),在那時(shí)候不需要用上Q,所以沒(méi)有深究。現(xiàn)在抱著學(xué)習(xí)的態(tài)度,重新試了1下,效果還不錯(cuò)。
A tool for making and composing asynchronous promises in JavaScript
Q是1個(gè)提供制作和創(chuàng)作異步Promise的JavaScript工具。Q 提供了1些輔助函數(shù),可以將Node和其他環(huán)境適配為promise可用的。
轉(zhuǎn)載保存: 《NodeJS Multiple Callback解決之使用Q Promises》
JavaScript Promise庫(kù) Q示例
官網(wǎng)給了1個(gè)簡(jiǎn)單的轉(zhuǎn)換的說(shuō)明
step1(function (value1) {
step2(value1, function(value2) {
step3(value2, function(value3) {
step4(value3, function(value4) {
// Do something with value4
});
});
});
});
將他轉(zhuǎn)換為
Q.fcall(promisedStep1)
.then(promisedStep2)
.then(promisedStep3)
.then(promisedStep4)
.then(function (value4) {
// Do something with value4
})
.catch(function (error) {
// Handle any error from all above steps
})
.done();
但是,我們沒(méi)有看懂,我們到底做了些甚么。。
JavaScript Promise 庫(kù) Q實(shí)戰(zhàn)
原生的代碼是這模樣的,用的是async庫(kù)
async.parallel([
function () {
'use strict';
pr.get(domain, next);
},
function () {
'use strict';
gs.get(name, next);
},
function () {
'use strict';
csdn.get(name, next);
},
function () {
'use strict';
zhihu.get(name, next);
},
function () {
'use strict';
alexa.get(domain, next);
}
]);
但是總感覺(jué)寫得有點(diǎn)亂,不過(guò)最少離開了所謂的回調(diào)大坑。
進(jìn)程大致上就是當(dāng)我們需要不斷往我們的result里面添加?xùn)|西。
因而將代碼改成Promise的情勢(shì),接著就變成這樣了
github.promise_get(response, name)
.then(function (result) {
return pr.promise_get(result, domain);
})
.then(function (result) {
return csdn.promise_get(result, name);
})
.then(function (result) {
return zhihu.promise_get(result, name);
})
.then(function (result) {
return alexa.promise_get(result, domain);
})
.then(function (result) {
callback(result);
});
但是這樣看上去寫得有點(diǎn)不好,由于我們將進(jìn)程固化在代碼中,因而試著,用別的方法對(duì)其重構(gòu)。
重構(gòu)的第1步后就變成這模樣
var info = Information.prototype;
info.pageRank_get = function(result){
'use strict';
return pageRank.promise_get(result, Information.prototype.domain);
};
info.alexa_get = function(result){
'use strict';
return alexa.promise_get(result, Information.prototype.domain);
};
info.csdn_get= function (result) {
'use strict';
return csdn.promise_get(result, info.name);
};
info.github_get= function (result) {
'use strict';
return github.promise_get(result, info.name);
};
info.zhihu_get = function (result) {
'use strict';
return zhihu.promise_get(result, info.name);
};
info.initVal = function (result) {
'use strict';
result = [];
return result;
};
Information.prototype.get = function (callback) {
'use strict';
Q.fcall(info.initVal)
.then(info.github_get)
.then(info.csdn_get)
.then(info.zhihu_get())
.then(info.pageRank_get)
.then(info.alexa_get)
.then(function (result) {
callback(result);
});
};
先提出每個(gè)方法,然后我們就能夠選擇我們需要用到的庫(kù)。看上去比上面整潔多了,但是我們還需要下1步,以便繼續(xù)。
生活不易,碼農(nóng)辛苦
如果您覺(jué)得本網(wǎng)站對(duì)您的學(xué)習(xí)有所幫助,可以手機(jī)掃描二維碼進(jìn)行捐贈(zèng)