5

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

1 Answer 1

8

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
});
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! Here is the obvious I was missing! My rss feed works now! great
Is items declared somewhere and called from within parseAndProcessFeed which does something like items.push(item)?

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.