0

I'm trying to wrap my head around the async library, but I'm pretty wobbly in NodeJs and I can't figure out async.parallel. The code below produces error TypeError: undefined is not a function on the line where the parallel tasks are to be executed. Am I correct in that tasks to be run in async.parallel should have a callback() when they are done? (irrelevant parts of the function are redacted)

function scrapeTorrents(url, callback) {
    request(url, function(err, res, body) {
        if(err) {
            callback(err, null);
            return;
        }
        var $ = cheerio.load(body);
        var results = [];
        var asyncTasks = [];
        $('span.title').each(function(i, element){
            // scrape basic info 
            var show = {title: info.title, year: info.year};
            asyncTasks.push(
                getOmdbInfo(show, function (err, res) {
                    if (res) {
                        omdbInfo = res;
                        results.push({
                            // add basic info and Omdb info
                        });
                    }
                    callback();
                })
            );
        });
        async.parallel(asyncTasks, function(){
            callback(null, results);
        });
    });
}

1 Answer 1

1

In the section where you define async tasks, be sure to specify a closure with a parameter method to call once the task is complete (named differently than callback so as to avoid hoisting).

asyncTasks.push(
    function (done) {
        getOmdbInfo(show, function (err, res) {
            if (err) {
                return done(err);
            }

            if (res) {
                omdbInfo = res;
                results.push({
                    // add basic info and Omdb info
                });
            }

            return done();
        })
    }
 );
Sign up to request clarification or add additional context in comments.

Comments

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.