Search code examples
javascriptnode.jshtmlexpressmeanjs

Access two remote resources synchronously and output it in combined response - NodeJS


In short.

I need to access two or more remote resource feeds, combine it and show it as one result from my nodejs service.


In detail

I need to fetch the feeds from multiple providers (which may vary in number according to what is stored in dashboard object)

Concatenate them, do some other data manipulations and show the content as one array at the end.

var allFeeds = [];
dashboard.providers.forEach(function(provider) {
  if (provider.source === 'facebook') {
    ...
    fb.getFeeds(provider.data.id, function(feeds) {
      ...
      Array.prototype.push.apply(allFeeds, feeds);
    });
  } else if (provider.source === 'google') {
    ...
    google.getFeeds(provider.data.id, function(feeds) {
      ...
      Array.prototype.push.apply(allFeeds, feeds);
    });
  } else if (provider.source === 'twitter') {
    ...
    twitter.getFeeds(provider.data.id, function(feeds) {
      ...
      Array.prototype.push.apply(allFeeds, feeds);
    });
  }
});
...
// other data manipulations
...
res.json(allFeeds);

As nodejs is having asynchronous network calls how can I achieve this?


Solution

  • You can use async.

    var async = require('async');
    var allFeeds = [];
    var tasks = [];
    
    dashboard.providers.forEach(function (provider) {
      if (provider.source === 'facebook') {
        ...
        tasks.push(function (done) {
          fb.getFeeds(provider.data.id, function (feeds) {
            ...
            Array.prototype.push.apply(allFeeds, feeds);
            done();
          });
        });
      } else if (provider.source === 'google') {
        ...
        tasks.push(function (done) {
          google.getFeeds(provider.data.id, function (feeds) {
            ...
            Array.prototype.push.apply(allFeeds, feeds);
            done();
          });
        });
      } else if (provider.source === 'twitter') {
        ...
        tasks.push(function (done) {
          twitter.getFeeds(provider.data.id, function (feeds) {
            ...
            Array.prototype.push.apply(allFeeds, feeds);
            done();
          });
        });
      }
    });
    
    async.parallel(tasks, function () {
      ...
      // other data manupulations
      ...
      res.json(allFeeds);
    });
    

    You can also check out this post I wrote to structure your code to better manage async operations