0

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();
    }
  );

1 Answer 1

1

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!

Sign up to request clarification or add additional context in comments.

3 Comments

Ok. In this case, how would I do something once I have processed all the things?
At the end of processItem() just call the function you need
That will call n times though, correct? I just want to call it once.

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.