0

When I try use async it is not defined:

async.whilst(func...);

Then I import it like so, but async is not a module it says:

var async = require('async');

So I then use npm to install it:

npm install async --save

But now when I run code I get error and I am not sure if I've installed the correct module:

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

TypeError: iteratee is not a function

Here is my full code...

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

I noticed that yipee! is being logged once and then the error happened.

Has anyone got an idea on how to fix this one?

Thanks in advance,

David.

Edit: Using @nico answer, I got working code: http://pastebin.com/ZCFstqsa

1
  • async.whilst expects two arguments. You are only passing one. The documentation is pretty clear: "Repeatedly call iteratee, while test returns true. Calls callback when stopped, or an error occurs." Commented Jan 5, 2017 at 17:57

1 Answer 1

3

That's because you're missing the second argument called iteratee
http://caolan.github.io/async/docs.html#whilst
You should do something like this instead
async.whilst(function allways() { return true; }, gameloop);

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for the help. The error is now gone, but the function gameloop only gets called once. Do you know why this might be happening?
That's because you need to call a callback when the execution finishes. The gameloop should look like this: function gameloop(cb) { console.log("yipee!"); cb();}
Thanks, I got the loop working. But there is a RangeError: Maximum call stack size exceeded. Is there a way to make it go on forever? Here is my updated code: pastebin.com/FAWjvQR8. It only execute for 9ms.
If you're running under a node.js environment you can call this instead of calling next directly: process.nextTick(next);
Just after figuring it out too. Thanks a million :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.