0

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);
  }
);
Vahid Kamyab
  • 55
  • 1
  • 5
  • 1
    Have a look at https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise – Lakshaya U. Aug 31 '21 at 16:56
  • 1
    TL;DR: the `Promise` constructor calls the function you pass to it, and it passes the `resolve` and `reject` functions to it when doing so. – deceze Aug 31 '21 at 16:56

0 Answers0