I have a question when I do the node.js async coding. Here is the example codes:
function foo(arg) {
var a = arg;
console.log(a + ' start');
setTimeout(function () {console.log(a);}, 500);
};
foo(1);
foo(2);
It outputs:
1 start
2 start
1
2
I'm confused. I thought it should output↓, because the local variable is changed by the foo(2)
1 start
2 start
2
2
Could you guys please tell me why/how node.js keep the local variable for the internal callback function access? Thanks a lot!