0

This is the function in question

function matches_password(password)
{
  var modified = false;
  var matched = false;

  User.count({password : password}, function (err, result)
  {
    modified = true;
    console.log('hei');
    if (err)
    {
      //error
    }
    else
    {
      if (result)
      {
        matched = true;
      }
    }
  });

  while (!modified)
  {
    console.log('sleeping');
    sleep.usleep(100000);
  }

  return matched;
}

As you can see, I have a callback that updates some variables; the main flow of execution is delayed until the callback is fired. The problem is that 'hei' never appears in the console.

Removing the while loop fixes the problem, but I need it, or else matched is always false.

What's going on and what can I do to get around this setback?

Valdrinit
  • 55
  • 6
  • Assuming you're using [this module](https://www.npmjs.com/package/sleep), `sleep.usleep` blocks, never letting the callback run. Besides that, see http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call – JohnnyHK Jul 18 '15 at 16:58

1 Answers1

-1

It looks like you're running into a asynchronous issue. you could try using a setTimeout function inside your while loop that calls User.count(...)

for how setTimeout works with Node: How does setTimeout work in Node.JS?

Community
  • 1
  • 1
goofiw
  • 19
  • 3