I have an object with the following form.
sortedFilters:
{
task:{...},
appointment:{...},
email:{...}
}
Now i want to use a for-in
loop, to build my tasks array for async.parallel
, which contains the functions to be executed asynchronously:
var tasks = [];
for (var entity in sortedFilters) {
tasks.push(function(callback) {
var entityResult = fetchRecordsForEntity(entity, businessUnits, sortedFilters);
var formattedEntityResults = formatResults(entity, entityResult);
callback(null, formattedEntityResults);
});
}
The problem is, that at the moment the functions are called, entity
points at the last value of the loop (in this case it would be email
).
How can i nail the exact value at the moment the function is added to the array?