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 of 10 will be printed 10 times. Obviously, it prints the value i
at the time the callback was executed instead of creating the callback. How to realize the desired result?
source
share