2
var responseArr = new Array();
async.each(response, function (value, k) {
    if(isDateFlag)
    {
         var defaultValue = value.auction_id;
         grpArray.push(value.title);
         var postData = {
             parent_id : parent_id,
             defaultValue : defaultValue,
             isDateFlag : isDateFlag,
             search_text : search_text
         }
         getChildNotificationList(postData, function (childArrayData) {
            //Creating the response array

             responseArr.push({
                'notification_id' : childArrayData['notification_id'], 
                'notification_text' : childArrayData['notification_text']
             });
        });

     }
});

return responseArr;//Blank Array

I want to return the final responseArr after manipulating it from child data query. It return blank array because it does not wait for the query response.

So how it can be async. Thanks

Mahesh Singh Chouhan
  • 2,558
  • 1
  • 16
  • 26
  • 3
    Possible duplicate of [How do I return the response from an asynchronous call?](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Parthapratim Neog Apr 04 '16 at 11:02
  • You should use `async.map` instead of `each`. And you need to call `k`. – Bergi Apr 04 '16 at 11:25
  • http://justinklemm.com/node-js-async-tutorial/ here is a neat example which you can follow... @Bergi: why async.map()?? why new array? – Thalaivar Apr 04 '16 at 11:31
  • @Thalaivar: Because you don't need `responseArr.push` with `map` – Bergi Apr 04 '16 at 11:32
  • @Bergi: how? you would need it... its something he is doing with is inner function some logic or params he is pushing... may be his code is not clearer for me to understand... – Thalaivar Apr 04 '16 at 11:35
  • 1
    @Thalaivar ... Thanks for the helpful tutorial. It works fine in the callback :) – Mahesh Singh Chouhan Apr 04 '16 at 11:59

1 Answers1

3

I referred http://justinklemm.com/node-js-async-tutorial/ and https://github.com/caolan/async.

This happens because the control goes on executing the code since javascript is synchronous. For getting the expected result modify the code as below:

var responseArr = new Array();
async.each(response, function (value, k) {
  if(isDateFlag){
    var defaultValue = value.auction_id;
    grpArray.push(value.title);
    var postData = {
      parent_id : parent_id,
      defaultValue : defaultValue,
      isDateFlag : isDateFlag,
      search_text : search_text
    }
    getChildNotificationList(postData, function (childArrayData) {
      //Creating the response array

      responseArr.push({
        'notification_id' : childArrayData['notification_id'], 
        'notification_text' : childArrayData['notification_text']
      });
      k();
    });
  } else {
    k();
  }
}, function (err) {
  if (err) {
    console.log(err);
  } else {
    return responseArr;
  }
});

The above code is inside a function block. You could get the result by calling the function.

Including the answer using async.map:

async.map(response, function (value, k) {
  if(isDateFlag){
    var defaultValue = value.auction_id;
    grpArray.push(value.title);
    var postData = {
      parent_id : parent_id,
      defaultValue : defaultValue,
      isDateFlag : isDateFlag,
      search_text : search_text
    }
    getChildNotificationList(postData, function (childArrayData) {

      k(null, {
        'notification_id' : childArrayData['notification_id'], 
        'notification_text' : childArrayData['notification_text']
      });
    });
  } else {
    k(null, {
      'notification_id' : '', 
      'notification_text' : ''
    });
  }
}, function(err, results){
  // results is now an array
  return results;
});
Madhukar
  • 196
  • 1
  • 7
  • It will always return array until the loop is going on.... I need to send it once after the all the child data pushed into the main response Array – Mahesh Singh Chouhan Apr 04 '16 at 11:08
  • I think you should specify that in your question by providing a proper code example. I have answered for the question you posted. You haven't mentioned about the looping. :) – Madhukar Apr 04 '16 at 11:10
  • code is starting with the async.each loop.. no need to mention it for the technical guys. – Mahesh Singh Chouhan Apr 04 '16 at 11:12
  • `k != callback`, and you should rather use `async.map` instead. Also none of your three `return`s make any sense. And you must not forget to call `k` in case `isDateFlag` is not set. – Bergi Apr 04 '16 at 11:26