質問

How to use async correctly with multiple dependent functions...

Here was my attempt which is not working out, it is within an async.waterfall function:

function (urlsCreated, cb) {
        var z, artist, title, added_on;
        z = [];
        async.mapSeries(urlsCreated, function (url, next) {
            scrape_music.total_pages(50, url, function (array, total, extra) {
                    scrape_music.each(artist, title, added_on, array, url, function (result) {
                    });
            });        
        }, function (z) {

            console.log(z);
        });
    }

Everything is working fine up to this part...

basically urlsCreated is an array of 2 urls...

Then I called a mapSeries assuming that it will iterate between them...

The way it should be working is, it iterates through each url in the array, then for each url it should get a total page count for the url, and then for each page count which is added to the array parameter/callback of the total_pages, should be iterated within...

so basically the arrays are: urlsCreated (containing 2 links) --> array (containing total pages in total_pages method) --> result (.each method should grab off each page, number of pages included in the array beforehand) and then repeat for amount of urls in urlsCreated...

Any help would be wonderful, currently nothing gets printed for z, and essentially I just want an array filled with objects that are returned in result from scrape_music.each.

EDIT----

Here is the code for those functions.

//loop thrugh each page and find jquery elements that match
    Scrape.prototype.each = function (artist, title, added_on, array, urls, cb) {
        console.log('entered each');
        console.log(array);
        var $trs, list;
        list = [];
        this.page(array, urls, function ($page) {
            //$trs selects all the rows from 1-50
            $trs = $page('tr').slice(11, -3);
            $trs.map(function (i, item) {  
                var result;
                result = {};
                result.artist = $page(item).find('td').eq(1).text();
                result.title = $page(item).find('td').eq(2).text();
                result.added_on = $page(item).find('td').eq(3).text();
                list.push(result);

            }); 

                cb(list);
        });

    };

    Scrape.prototype.total_pages = function (divide, url, cb) {
        return request("" + url + config.url.pageQ + 0, function (err, res, body) {
                if (err) { throw err; }
                var page, select, match, total, matches, array, extra;
                array = [];
                page = cheerio.load(body);
                select = page('tr').slice(9, 10);
                match = page(select).find('td').eq(1).text();
                matches = match.slice(-18, -14).trim();
                total = Math.round(matches / divide);
                extra = matches % divide;
                for(x = 0; x < total; x++) {
                    array.push(x);
                }
                cb(array, total, extra);         
         });
    };

     //used to loop through all pages
    Scrape.prototype.page = function (array, urls, cb) {
        return array.forEach(function (i) {
            return request("" + urls + config.url.pageQ + i, function (err, res, body) {
                //console.log(urls + config.url.pageQ + i);
                if (err) { throw err; }
                cb(cheerio.load(body));
            });
        });
    };
役に立ちましたか?

解決

function (urlsCreated, cb) {
    var artist, title, added_on;

    async.mapSeries(urlsCreated, function (url, next) {
        scrape_music.total_pages(50, url, function (array, total, extra) {
            // 1:
            scrape_music.each(artist, title, added_on, array, url, function (result) {
                // 2:
                next(null, result);
            });
        });        
    }, function (err, z) {
        // 3:
        console.log(z);
    });
}
  1. each() here can't be an iterator (not sure what it does) as you can only call next() for asyncMap once per iteration. If the callback is called when the iterating is done then it's fine
  2. Tell async this iteration is done. The first argument is any error
  3. The second argument is the new array
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top