2012-09-02 15 views
0

我有這個簡單的nodejs應用程序,它爲我的web應用程序生成虛擬日期。 它所做的就是:尋找在nodejs應用程序中管理異步流的主流方式

  1. 丟棄虛擬數據庫
  2. 填充庫存收集
  3. 填充發票收集
  4. 填充常量數據收集

當然,所有的動作是異步的,我想依次執行它們,一個接一個。對我而言,寫一些東西來管理這種流程更簡單,但是,我想要一個支持其他類型流的主流解決方案。例如,並行運行並在第一次失敗時停止運行。

供您參考,請找骨架下面,描繪我的解決方案:

/*global require, console, process*/ 

var mongo, db, inventory, createChain; 

function generateInventory(count) { 
    // returns the generated inventory 
} 

function generateInvoices(count, inventory) { 
    // returns the generated invoices 
} 

function generateConst() { 
    // returns the generated const data 
} 

mongo = require('mongojs'); 
db = mongo.connect('dummy', ['invoices', 'const', 'inventory']); 

createChain = function() { 
    "use strict"; 
    var chain = [false], i = 0; 

    return { 
    add: function (action, errMsg, resultCallback) { 
     chain[chain.length - 1] = {action: action, errMsg: errMsg, resultCallback: resultCallback}; 
     chain.push(false); 
     return this; 
    }, 
    invoke: function (exit) { 
     var str, that = this; 
     if (chain[i]) { 
     chain[i].action(function (err, o) { 
      if (err || !o) { 
      str = chain[i].errMsg; 
      if (err && err.message) { 
       str = str + ": " + err.message; 
      } 
      console.log(str); 
      } else { 
      if (chain[i].resultCallback) { 
       chain[i].resultCallback(o); 
      } 
      i += 1; 
      that.invoke(exit); 
      } 
     }); 
     } else { 
     console.log("done."); 
     if (exit) { 
      process.exit(); 
     } 
     } 
    } 
    }; 
}; 

createChain() 
    .add(function (callback) { 
    "use strict"; 
    console.log("Dropping the dummy database."); 
    db.dropDatabase(callback); 
    }, "Failed to drop the dummy database") 
    .add(function (callback) { 
    "use strict"; 
    console.log("Populating the inventory."); 
    db.inventory.insert(generateInventory(100), callback); 
    }, "Failed to populate the inventory collection", function (res) { 
    "use strict"; 
    inventory = res; 
    }) 
    .add(function (callback) { 
    "use strict"; 
    console.log("Populating the invoices."); 
    db.invoices.insert(generateInvoices(10, inventory), callback); 
    }, "Failed to populate the invoices collection") 
    .add(function (callback) { 
    "use strict"; 
    console.log("Populating the const."); 
    db["const"].insert(generateConst(), callback); 
    }, "Failed to populate the const collection") 
    .invoke(true); 

任何人都可以提出一個有關包的NodeJS,這也將是容易使用?

非常感謝。

回答

2

使用async模塊可以提供您可能需要的任何類型的流量控制。特別是,series方法提供順序流量控制。

+0

小心顯示如何使用異步重寫我的代碼? – mark

+2

我是第二個異步模塊,我非常喜歡它。如果你有興趣,我可以在這裏談一談:http://nodecasts.net/episodes/5-thinking-asynchronously –

0

實際上,對於連續的流量控制,你應該使用瀑布

舉個例子:

async.waterfall([ 
    function(cb){ 
    cb(null,1); 
    }, 
    function(r,cb){ 
    // r=1 
    cb(null,2) 
    }, 
    function(r,cb){ 
    // r=2 
    cb(null,3) 
    } 
],function(e,r){ 
    // e=null 
    // r=3 
}) 

這將順序執行。 如果您提前回調錯誤(即cb(「error」)),那麼它將直接進入最終函數(e,r),其中e =「error」和r = undefined 注意函數(r, cb){}可以在util庫中被預先組合,以處理通常重用的塊,並使未來更容易。