ianks
ianks

Reputation: 1768

Returing result of asynchronous function within Async.map

I'm very new to JS and functional programming in general and am struggling to find a graceful solution to this problem. Essentially, I want to make async requests to a MongoDB server, and return the results to an async to map function. The problem I am having in that the actual function within async.map is asynchronous itself. I would like to know a graceful solution here, or at least get a pointer in the right direction! Thanks!

  async.map(subQuery,
    function(item){
      collection.distinct("author", item, function(err, authors){
        counter++;
        console.log("Finished query: " + counter);

        var key = item['subreddit'];
        return { key: authors };
      })
    },

    function(err, result){
      if (err)
        console.log(err);
      else{
        console.log("Preparing to write to file...");

        fs.writeFile("michaAggregate.json", result, function() {
          console.log("The file was saved!");
        });
      }

      db.close();
    }
  );

Upvotes: 0

Views: 120

Answers (1)

Maris
Maris

Reputation: 4776

You should process item only when the data is fetched. Just use callback That the common way of JavaScript. Like this:

var processItem = function(item){  
// Do some street magic with your data to process it

// Your callback function that will be called when item is processed.
onItemProccessed();
}

async.map(subQuery,
function(item){
  collection.distinct("author", item, function(err, authors){
    counter++;
    console.log("Finished query: " + counter);

    var key = item['subreddit'];
    processItem(item);
  })
},

function(err, result){
  if (err)
    console.log(err);
  else{
    // That string added **ADDED** 
    console.log('HEEY! I done with processing all data so now I can do what I want!');
    console.log("Preparing to write to file...");

    fs.writeFile("michaAggregate.json", result, function() {
      console.log("The file was saved!");
    });
  }

  db.close();
}
);

ADDED

By the specification of async.map you can see:

https://github.com/caolan/async

async.map(arr, iterator, callback):

callback(err, results) - A callback which is called when all iterator functions have finished, or an error occurs. Results is an array of the transformed items from the arr. As you see that callback is exactly what you need!

Upvotes: 1

Related Questions