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

6
  • 3
    Possible duplicate of How do I return the response from an asynchronous call? Commented Apr 4, 2016 at 11:02
  • You should use async.map instead of each. And you need to call k. Commented Apr 4, 2016 at 11:25
  • justinklemm.com/node-js-async-tutorial here is a neat example which you can follow... @Bergi: why async.map()?? why new array? Commented Apr 4, 2016 at 11:31
  • @Thalaivar: Because you don't need responseArr.push with map Commented Apr 4, 2016 at 11:32
  • 1
    @Thalaivar ... Thanks for the helpful tutorial. It works fine in the callback :) Commented Apr 4, 2016 at 11:59

1 Answer 1

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;
});
Sign up to request clarification or add additional context in comments.

4 Comments

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
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. :)
code is starting with the async.each loop.. no need to mention it for the technical guys.
k != callback, and you should rather use async.map instead. Also none of your three returns make any sense. And you must not forget to call k in case isDateFlag is not set.

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.