Javascript promises are giving me some unexpected behavior where they output in a synchronous and expected way when debugging with breakpoints but appear to be doing something strange and asynchronous when running otherwise. Here is my code:
function aPromise() {
return new Promise(function(resolve, reject) {
console.log("making a promise")
bPromise().then(function() {
console.log("resolving a promise")
resolve();
});
});
}
function bPromise() {
return new Promise(function(resolve, reject) {
console.log("making b promise")
console.log("resolving b promise")
resolve();
});
}
function logDone() {
console.log("done");
}
aPromise().then(logDone)
aPromise().then(logDone)
The output to console of the above is this:
making a promise
making b promise
resolving b promise
making a promise
making b promise
resolving b promise
resolving a promise(2)
done(2)
Which I find strange. Stranger still is when I set a break point and step through line for line, the output is what I would expect:
making a promise
making b promise
resolving b promise
resolving a promise
done
making a promise
making b promise
resolving b promise
resolving a promise
done
Why is this happening and how do I fix it?
I'm running an up to date Firefox 45.0.1. It also appears to be happening in Chrome.