I was playing around with this asynchronous code and while is expected that the callback will be executed after 1 ms after the setTimeout and the value of val will be the value that it has at that time, so I tried to increase the reassignments of val hoping to overcome the 1ms of delay, until the execution of callback, but no matter how many reassignments of val I add, the value of val is always the one of the last assignment. So the question is, does all these reassignments happen so quickly that 1ms is enough to execute them all before executing the callback or am I missing something here?
function asyncFunction(callback) {
setTimeout(callback, 1);
}
var val= '1';
asyncFunction(function() {
console.log('The value is ' + val);
});
val= '2';
val= '3';
//...
//... more asignments
val = '1000'