You have a g
variable you check against 0
but you did not define g
anywhere.
The reason you did not see this is that native (or jQuery) promises do not automatically track possibly unhandled rejections.
You can check for errors by appending a .catch
to the end of the chain and see if anything went wrong.
q.then(function (result) {
...
}).catch(function(e){
console.log(e.message);
console.log(e.stack);
});
Which would have shown you the issue.
Alternatively, use a stronger library like Bluebird that will alert you in the case of unhandled rejections.