javascriptscopeglobal-variableshoodie

JavaScript declared global variable can't assign in method


I have some JavaScript code where I have a variable that I've declared outside the function however I can't seem to assign it.

I've looked around and am doing the generally accepted thing of assigning outside the function but still have issues.

Any ideas?

var numberPeopleSignedIn;

function setWaitTime(id, currentTimeToMinus){

var peopleAvailable = [];

  hoodie.store.findAll('barber')
  .done(function(object) {
      object.forEach(function(barber) {
      if (barber.statusInOut == "signedIn") {
        peopleAvailable.push(barber);
      }
      numberPeopleSignedIn = peopleAvailable.length;
    });

  });


console.log(numberPeopleSignedIn);  //This is returning undefined

}

Solution

  • findAll() is returning a delegate. Meaning the console.log is being called before the .done() function when numberPeopleSignedIn is still undefined.