I'm learning Promise in JavaScript and just wonder how does it work behind the scenes?
As you see in the example there is only an anonymous function we pass to Promise constructor and no calling in the reminder of code.
If there wasn't any .this
was it sitll being called and changed the state and value of myPromise
object?
let myPromise = new Promise(function(myResolve, myReject) {
let x = 0;
if (x == 0) {
myResolve("OK");
} else {
myReject("Error");
}
});
myPromise.then(
function(value) {
console.log(value);
},
function(error) {
console.log(error);
}
);