I came across a fragment similar to this
(function(){
for(var i=0;i<3;i++){
setTimeout(function(){
console.log(i)
})
}
}())
I expected it to write 1,2 .... instead it is logged 3. I am not sure if this is because js beign single threaded, & looking in line only after the loop is complete.
WORKING COPY
source
share