2017-01-05 111 views
-1

當我嘗試異步沒有定義使用:節點JS異步工作不

async.whilst(func...); 

然後,我將其導入像這樣,但異步是不是說,一個模塊:

var async = require('async'); 

所以然後我用npm進行安裝:

npm install async --save 

但現在當我運行代碼,我得到的錯誤,我不知道我是否已經安裝了心病矩形模塊:

.../node_modules/async/dist/async.js:4960 
    iteratee(next); 
    ^

TypeError: iteratee is not a function 

這裏是我的全部代碼...

var async = require('async'); 
async.whilst(gameloop); 

function gameloop() 
{ 
    // I will be adding code here to make it run at 30fps, and use deltatime. 
    // This will be a gameloop for the multiplayer game I am creating. 
    console.log('yipee!'); 
    return true; 
} 

我注意到,yipee!被記錄一次,然後發生錯誤。

有沒有人知道如何解決這個問題?

在此先感謝,

David。

編輯:使用@nico答案,我得到了工作代碼:http://pastebin.com/ZCFstqsa

+0

'async.whilst'需要兩個參數。你只能通過一個。 [documentation](http://caolan.github.io/async/docs.html#whilst)非常清楚:*「反覆調用'iteratee',而'test'返回true。停止時調用'callback',或者發生錯誤。「* –

回答

3

那是因爲你錯過了第二個參數稱爲iteratee
http://caolan.github.io/async/docs.html#whilst
你應該做這樣的事情,而不是
async.whilst(function allways() { return true; }, gameloop);

+0

感謝您的幫助。現在錯誤消失了,但函數'gameloop'只被調用一次。你知道爲什麼會發生這種情況嗎? –

+0

你可以在這裏寫下你當前的源代碼嗎? – nico

+0

http://pastebin.com/uPbeemKQ –