NodeJS Multiple Callback解決之使用Q Promises

來源:互聯網
上載者:User

標籤:nodejs   asynchronous   promise   callback   

在上一篇《Javascript Promises模式——相當酷的Callback Hell終結者》我們介紹了Javascript的Promise模式,接著我們就把Javascript Promise用到我們的代碼中。

JavaScript Promise庫 Q

之前試著用過Q,但是沒有成功。或者說,在那時候不需要用上Q,所以沒有深究。現在抱著學習的態度,重新試了一下,效果還不錯。

A tool for making and composing asynchronous promises in JavaScript

Q是一個提供製作和創作非同步Promise的JavaScript工具。Q 提供了一些輔助函數,可以將Node和其他環境適配為promise可用的。

轉載保留: 《NodeJS Multiple Callback解決之使用Q Promises》

JavaScript Promise庫 Q樣本

官網給了一個簡單的轉換的說明

step1(function (value1) {    step2(value1, function(value2) {        step3(value2, function(value3) {            step4(value3, function(value4) {                // Do something with value4            });        });    });});

將他轉換為

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();

但是,我們沒有看懂,我們到底做了些什麼。。

JavaScript Promise 庫 Q實戰

原生的代碼是這樣子的,用的是async庫

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);    }]);

但是總感覺寫得有點亂,不過至少離開了所謂的回調大坑。

過程大致上就是當我們需要不斷往我們的result裡面添加東西。

於是將代碼改成Promise的形式,接著就變成這樣了

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);    });

但是這樣看上去寫得有點不好,因為我們將過程固化在代碼中,於是試著,用別的方法對其重構。

重構的第一步後就變成這樣子

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);        });};

先提出每一個方法,然後我們就可以選擇我們需要用到的庫。看上去比上面整潔多了,但是我們還需要下一步,以便繼續。

NodeJS Multiple Callback解決之使用Q Promises

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.