简体   繁体   English

不为async.parallel调用完整的回调

[英]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). 我需要对100行进行api调用以填充描述(我更愿意并行执行)。 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. 但是,在这种情况下,某些行可能没有描述,因此api将返回404。当存在一行或多行没有描述的行时,我需要显示一条警告消息,并将这些行从模式数据中删除,这意味着我需要完整的回调或完成的回调。 However the completeCallback is not being called and I "think" it's because some of rows doesn't have description. 但是,completeCallback没有被调用,我“认为”这是因为某些行没有描述。

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. 如果您不执行回调, async将假定您的函数尚未完成。 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));
    }
  });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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