I have to iterate through a given JSON object and create a task for each object in there. The given tasks require information from this JSON object as well and I wonder how I can pass these information to my task so that it is available when executed.
Building my task array:
var asyncScrapeTasks = [];
var resources = JSON.parse(body);
for(var i=0; i<resources.items.length; i++)
{
asyncScrapeTasks.push(function (callback)
{
console.log(resources.items[i].id);
});
}
Executing my tasks:
async.parallelLimit(asyncScrapeTasks, 5, function() {
callback(null, "Done");
});
My problem:
Right now console.log(resources.items[i].id); returns undefined, which makes sense for me, because the index i is not known at the time the functions being executed, but I wonder how I can solve my problem.
because resources i not known at the time the functions being executedmakes no sense whatsoever based on the example. If that is REALLY what is happening then the example you've posted is the wrong one (you've simplified it so much as to completely eliminate the problem)