2017-10-05 39 views
0

大家好,如何推動幾個函數ARGS到一個數組

在Node.js的模塊(自定義隊列)我的工作, 我現在用的是queue module,我需要推入其數組「作業」帶有參數的多個函數(估計時間爲int) 當我啓動隊列時,出現一個錯誤,指出作業不是函數。 我想我明白了爲什麼,這是因爲當我按下它時,會執行「process」函數。但是我需要稍後用參數執行這個過程。

我的代碼:

module.exports = Queue 
var process = require("../test/processjob") 
var q = require("queue"); 

function Queue(options) { 
    if (!(this instanceof Queue)) { 
    return new Queue(options); 
    } 
    options = options || {} 
    this.queue = q(); 
    /* 
handling other options 
*/ 
} 

Queue.prototype.processJob = function() { 

    for (var i = 0, len = this.tasks.length; i < len; i++) { 

    this.queue.push(process.process(this.tasks[i].estimated_time));// <== push here 
} 
this.queue.start(); //<== exception here 
} 

非常感謝,爲我的英文不好對不起。

回答

0

要推一個功能到一個數組,你會希望在稍後的時間點來執行,你可以換用另一個函數,函數,即:

this.queue.push(
    function(cb) { 
    process.process(this.tasks[i].estimated_time) 
    cb(); 
    } 
);// <== push here 

或者使用ES6

this.queue.push((cb) => { 
    process.process(this.tasks[i].estimated_time); 
    cb(); 
}); 
+0

所以,我試過你的解決方案,它似乎部分工作,異常消失了,但是當一個隊列開始([queue.start()](https://www.npmjs.com/package/隊列#qstartcb))這些作業似乎沒有處理。 –

+0

好吧,讓我看看你提到的隊列庫,看看你的實現中是否有什麼問題 – linasmnew

+0

試試我更新的答案 – linasmnew