Stumped on this, sure there is an elegant way to do this but not sure what.
I would like something like:
let x = 5;
const p = new Promise((resolve, reject) => {
setTimeout(() => {
resolve();
}, 2000);
}).then(() => {
console.log(x);
});
x = 3;
// Print out 5 after 2 seconds.
Basically, given a setup similar to above, is there a way to print out '5' regardless of whether the value of x is changed during the async timeout? In my case, it would be hard to simply pass x in the resolve().