1

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.

4
  • Your code is sort of incomplete since console.log is not an async function. Can you give a more concrete example of what you're trying to do? Commented Jul 21, 2016 at 4:46
  • @slebetman yes I tried to simplify it as much as possible. Basically I want to get a json object from an external URL and fetch specific information from this json file in order to store it in my own database. Commented Jul 21, 2016 at 4:51
  • But your comment because resources i not known at the time the functions being executed makes 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) Commented Jul 21, 2016 at 5:29
  • @slebetman you were right and I have figured out what the problem is, but I still don't know how to solve it. I have updated my original question. It is about the index of my iteration instead of the variable resources. Commented Jul 21, 2016 at 5:42

1 Answer 1

2

As I don't have enough reputation to add comment, I am writing as answer. Sorry.

Is there any specific reason to use paralleLimit? async.js have each which could iterate collection and perform operation on each item. This function applies the function iteratee to each item in collection, in parallel. async.js each

If you want to limit the operations: async.js eachLimit

Sign up to request clarification or add additional context in comments.

2 Comments

I am not 100% convinced that I got the main difference between each and parallel? I just read that parallel is kicking off I/O tasks parallel which is exactly what I am doing as I am requesting information via an URL call to an external webserver. I think your point is that I could iterate on resource.items.eachLimit directly, is this correct? I am new to async.js
Yes, you can use eachLimit, in iteratee you can call your rest api. asyc.js Documentation have described other flavor and also provided the link of source below each example.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.