Return value in a async.forEach in node js?

async.js, node.js

Solution

You can't return value from asyncronious function. You need pass it into callback function. Like this:

function aggregate(topic, callback) { 
  async.forEach(topic.feeds, 
    function(item, callback) {
      parseAndProcessFeed(item, callback);
    }, 
    function(err) {
      // sort items by date
      items.sort(function(a, b) {
       return (Date.parse(b.date) - Date.parse(a.name));
      });
      var rssFeed = createAggregatedFeed();
      callback(err, rssFeed);
    }
  );
}

aggregate(someTopic, function(err, result) {
    // here is result of aggregate
});

Problem

I'm starting to learn node.js, and to aggregate multiple rss feeds in a single one, I'm fectching the feeds and then recreate a unique feed from the data I fecthed. So, in order to handle multiple http requests asynchronously I use https://github.com/caolan/async#forEach which does the job. But I can't figure how to return a value (the rss xml feed in my case). Here is my code : ``` function aggregate(topic) { async.forEach(topic.feeds, function(item, callback) { parseAndProcessFeed(item, callback); }, function(err) { // sort items by date items.sort(function(a, b) { return (Date.parse(b.date) - Date.parse(a.name)); }); var rssFeed = createAggregatedFeed(); console.log(rssFeed); return rssFeed; } ); } ``` with `console.log(rssFeed)` I can see the rss feed, so I think I'm missing something obvious. How can I return the `rssFeed` value? Some help to get me back on seat would be great, thanks! Xavier

Original source