I hope this question is not a duplicate, I have searched similar question here but got no matching result.
In node.js Below code is polluting the storage variable. And I really can't figure out why.
var _ = require('underscore'); // same result by using lodash
function add(arr, callback) {
var l = arr.length,
storage = [];
function doCallback() {
callback(storage);
}
returnStorage = _.after(l, doCallback);
_.each(arr, function (a) {
_.delay(function () {
storage.push(a);
returnStorage();
}, 10);
});
}
function callbackHandler(result) {
console.log(result);
}
add([1,2,3], callbackHandler),
add([4,5,6,7], callbackHandler);
// in console will print wrong result:
// [ 4 ]
// [ 4, 5 ]
// [ 4, 5, 6 ]
// [ 4, 5, 6, 7 ]
However if I don't use _.after() it will give the expected result. Using underscore or lodash give same result.
Below code is working fine.
var _ = require('underscore'); // same result by using lodash
function add(arr, callback) {
var l = arr.length,
storage = [];
function returnStorage() {
if (storage.length == l) {
callback(storage);
}
}
_.each(arr, function (a) {
_.delay(function () {
storage.push(a);
returnStorage();
}, 10);
});
}
function callbackHandler(result) {
console.log(result);
}
add([1,2,3], callbackHandler),
add([4,5,6,7], callbackHandler);
// in console will print correct result:
// [ 1, 2, 3 ]
// [ 4, 5, 6, 7 ]
How should i identify the root cause !