What is the output of the following code? let x = 0; const p = new Promise(resolve => { x = 1; resolve(x); x = 2; }); p.then(v => console.log(v, x));

JavaScript Professional Hard

JavaScript Professional — Hard

What is the output of the following code? let x = 0; const p = new Promise(resolve => { x = 1; resolve(x); x = 2; }); p.then(v => console.log(v, x));

Key points

  • The Promise is resolved with the current value of x, which is 1.
  • The value of x is changed to 2 after the Promise is resolved.
  • The then() method logs the resolved value and the current value of x.

Ready to go further?

Related questions