javascript - 如何在 JavaScript 或 Node 中使其异步?

标签 javascript node.js asynchronous

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

我想在从子数据查询中操作它之后返回最终的responseArr。它返回空白数组,因为它不等待查询响应。

那么它是如何异步的呢?谢谢

最佳答案

我提到了http://justinklemm.com/node-js-async-tutorial/https://github.com/caolan/async .

发生这种情况是因为控件继续执行代码,因为 javascript 是同步的。为了获得预期的结果,修改代码如下:

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;
  }
});

以上代码位于功能 block 内。调用该函数即可得到结果。

使用 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;
});

关于javascript - 如何在 JavaScript 或 Node 中使其异步?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36400760/

相关文章:

javascript - 如何在类的实例中声明类的实例?

javascript - WordPress 上的病毒 wp-tmp.php 可以做什么

javascript - 无法从 iframe 内部将边框应用于 iframe

node.js - react 和 Socket.io : Able to get initial data - but view doesn't update when a new post comes in

javascript - Node.js nwjs包编译错误: Invalid file descriptor to ICU data

ios - 快速异步加载多个 View

javascript - 通过 DOM 将文本添加到 JavaScript 中的现有文本元素

node.js - 如何将本地主机连接到域?

asynchronous - 如何在 Angular2 循环/映射中执行异步 HTTP 请求并修改原始循环数组?

javascript - 尝试在 Node 中运行同步 REST 请求时出现问题