I'm trying to understand what is going on here. I am trying to change a variable defined outside of a function scope. I come from a Java background and this set of code has taken me by surprise
var dayIsEmpty = false;
selectByDate.on('end', function(result) {
if(result.rowCount == 0) {
console.log('isEmpty');
dayIsEmpty = true;
} else {
console.log('isNotEmpty');
dayIsEmpty = false;
}
console.log('isEmpty?' + dayIsEmpty);
console.log('Finished Select from UsageByDay');
});
console.log('outside of block isEmpty?' + dayIsEmpty);
I noticed that
outside of block isEmpty? false
is called before any of console output defined within the function is called. First of all... what is going on? And secondly, how would you work around this.