0

My goal is to create a JSON object, from a paragraph of text, that I can then insert as a document into MongoDB. I'm using nodejs and wanted to go for the async approach.

My JSON has parameters like so

{
   height:height,
   weight:weight
}

My logic is this

create a module with async functions that parse the text and extract weight and height using regex.

but then how would I combine all the responses from these functions into one JSON that I can import at once?

I'm thinking something like this

var get_height = require().height;
var get_weight = require().weight;

exports.contr = function(){
   var height,
       weight;
   get_height(text, function(err, res){
      if(err)
          throw(err)
      height=res;
   });

   get_weight(text, function(err, res){
      if(err)
          throw(err)
      weight=res;
   });
   //All other async functions
   combine_json(height, weight, ... , function(err, res){
       if(err)
          throw(err);

       console.log(res); //the json was successfully inserted into mongoDB
   }); 
}

I find async confusing and in the above example I'm not sure about two things

  1. wouldn't combine_json run without waiting for the data from the previous two functions (weight, height)

  2. what is the best practice to handle such cases? Should i just use sync functions and wait top-to-bottom for each one to do its thing and then run the final one or I can leverage async?

3 Answers 3

1

The simplest way to wait for the results of two independent asynchronous functions is to use promises and Promise.all. For this we'll assume get_height and get_weight return a Promise and can be used as such:

get_height().then(function (height) { console.log(height); });

Then it's trivial to combine two of those promises:

Promise.all([get_height(), get_weight()]).then(function (results) {
    combine_json(results[0], results[1]);
});

See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise for documentation and details.

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

1 Comment

just a 'callback' that these promises are totally what i needed but they are hard to wrap my head around them. Thanks for the response, man.
0

If you do not know nothing of Promises, you first should know how callbacks works. If you do not want a elegant solution with Promise.all() and you just want your code working, you need nested functions. When you are inside get_height callback you should to call get_weight and same when you are inside get_weight callback you should call combine_json() . The only problem is that you have to wait for get_height to call get_weight. This is solved with Promise.all() as it's said.

   get_height(text, function(err, height){
      if(err)
          throw(err);
      get_weight(text, function(err, weight){
         if(err)
            throw(err);
         //All other async functions
         combine_json(height, weight, ... , function(err, res){
             if(err)
                throw(err);
             console.log(res); //the json was successfully inserted into mongoDB
         }); 
      });
   });

1 Comment

I said in my answer that thisis the problem.
0

Promises are your best bet, but if you don't want to use them for some reason and prefer the callback style, then

function get_height_and_weight(text, callback) {
  var have_height = false;
  var have_weight = false;
  var result = {};

  get_height(text, function(err, height) {
    if (err) callback(err);
    have_height = true;
    result.height = height;
    if (have_weight) callback(null, result);
  });

  get_weight(text, function(err, weight) {
    if (err) callback(err);
    have_weight = true;
    result.weight = weight;
    if (have_height) callback(null, result);
  });
}

This is a special case of the parallel async call case which could be handled better by async.parallel.

Comments

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.