简体   繁体   中英

complete callback is not being called for async.parallel

I need to make an api call for 100 rows to populate description (which I prefer to do it in parallel). However some of rows might not have description in this case api will return 404. I need to show a warning message when there are a row or rows without description and remove those rows from modal data which means I need a complete callback or done callback. However the completeCallback is not being called and I "think" it's because some of rows doesn't have description.

Could you please tell me how to achieve that?

Here is my code:

    function getDescription(processedData) {
        $.ajax({
            url: baseApiUrl + '/Summary?id=' + processedData.id,
            type: 'GET',
            dataType: 'json',
            contentType: 'application/json',
            success: function (data) {
                processedData.SummaryDescription = data;
            },
            error: function (xhr, status, e) {
                if(xhr.statusCode === 404){
                    processedData.SummaryDescription = '';
                }else{

                }
            }
        });
    };

//Below line is in a look
parallelCallArray.push(getDescription.bind(null, processedData));

//Below line is out of loop
Async.parallel(parallelCallArray, function(err, result){
  console.log('all calls completed...');
}); 

You're missing the callback parameter of your function(s) that are being executed in parallel. If you don't execute the callback, async will assume your functions haven't finished yet. Try something like this:

function getDescription(processedData, cb) {
  $.ajax({
    url: baseApiUrl + '/Summary?id=' + processedData.id,
    type: 'GET',
    dataType: 'json',
    contentType: 'application/json',
    success: function (data) {
      processedData.SummaryDescription = data;
      cb();
    },
    error: function (xhr, status, e) {
      if (xhr.statusCode === 404) {
        processedData.SummaryDescription = '';
      } else {
      }
      cb(new Error(e));
    }
  });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM