Using node-mysql I have the following code:
for (var i = 0; i < 10; i++) {
connection.query('select 1', function(err, rows) {
console.log('#' + i);
});
}
I expected the result to be #0, #1, ..., #9 but the actual result is #10 printed 10 times. It is obvious it is printing the value of i
at the moment of the callback's execution instead of the callback creation. How can I implement my desired result?