简体   繁体   中英

NodeJs Async Parallel: 'undefined is not a function'

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);
        });
    });
}

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();
        })
    }
 );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM