Thursday, 11 May 2017

Async each iterating issue in Node

I'm trying to parallelize objects creation for each rss link i have in an array.

I tried to use async.each() for that in order to get an array containing all objects, but i only (and always) get an array containing objects from the last rss link.

Here is my code:

async.each(rss, parsefeeds, function(err){
    if (err) console.log('ERROR', err);
    res.render('index', {articles: tab});
});

function parsefeeds(link, callback) {
    feed(link, function(err, articles) {
        if (err) throw err;
        var articles_length = articles.length;

        for (var i = 0; i < articles_length; i++) {
            var publication = moment(articles[i].published).subtract(1, 'hours');
            articles[i].published = publication.format('dddd D MMM HH:mm');
            if (articles[i].feed.name.indexOf(':') > -1) {
                var temp_tab = articles[i].feed.name.split(':');
                articles[i].feed.name = temp_tab.join('');
            }
        }
        tab = articles;
        callback(err);
    });
};

I've already make some research in Stackoverflow, people often advice to use async.eachSeries() instead of async.each() but with my code, the result is the same.

I also tried to use async.waterfall() inside my async.each() but no result again.

Any idea? Did I miss something ?



via LeyluIAA

No comments:

Post a Comment