2014-01-25 76 views
0

我有一組函數,我需要以同步方式執行(在完成之後,我使用前一個函數的結果調用另一個函數)。每個函數都以字典爲參數。 是否有任何模塊已經完成node.js或JavaScript?異步執行的函數隊列

+0

https://github.com/kriskowal/q,https://github.com/caolan/async –

回答

0

您可以使用ES5 Array.reduce

function queueFunctions(funcs, initialValue, commonData) { 
    funcs.reduce(function(previousValue, currentValue/*, index, array*/){ 
     return currentValue(previousValue, commonData); 
    }, initialValue); 
} 

例如:

queueFunctions([prompt, confirm, alert], 'foo?');