【问题标题】:Async call in nodejs using promise使用promise在nodejs中进行异步调用
【发布时间】:2020-02-08 11:13:48
【问题描述】:

我需要使用 php 发送批量短信,因此 nodejs 与套接字连接我正在使用套接字向 nodejs 发送数据以发送批量短信,并且所需的结果作为发送项,表示我成功发送了多少条短信将响应推送到数组中,然后我会将数组发送给客户端以进行进一步计算,但问题是 nodejs 同步工作,所以我使用 promise 异步执行此工作,但数组为 null

function getData(smsDetails) {
  var results = [];
  var sendLocalSmsPromis = new Promise(function (resolve, reject) {
    for (var a = 0; a < smsDetails.data.contacts.length; a++) {
      var contact = smsDetails.data.contacts[a].phone;
      var dialCode = contact.substring(0, 2);
      if (dialCode == 92) {
        localSms(smsDetails.data.message, contact, smsDetails.data.senderId.name, function (response) {
          if (response.orginalResponse.Data.hasOwnProperty("id")) {
            //sent successfully
          } else {
            var smsLength = generalHelper.calculateSmsLength(response.message);
            var sentItemsData = {
              user_id: smsDetails.data.user_id,
              number: response.contact,
              message: response.message,
              length: smsLength,
              status: 0,
              type: 'portal',
              date: moment().format("YYYY-MM-DD HH:ss:mm")
            };
            results.push(sentItemsData);
          }

        });

      }

    }
    resolve(results);
  });

  return sendLocalSmsPromis;
}


socket.on("sendSms", function (smsDetails) {

  var data = getData(smsDetails);
  data.then(function (response) {
    console.log(response);
    // socket.emit("smsResponse",response);
  }, function (err) {
    console.log(err)
  });
});

【问题讨论】:

    标签: php node.js socket.io


    【解决方案1】:

    localSms 函数是异步的,for-loop 是同步的。
    将您的 localSms 转换为一个承诺返回函数。

    function getData(smsDetails) {
        var results = [];
        for (var a = 0; a < smsDetails.data.contacts.length; a++) {
            var contact = smsDetails.data.contacts[a].phone;
            var dialCode = contact.substring(0, 2);
            if (dialCode == 92) {
                results.push(localSmsPromisified(smsDetails.data.message, contact, smsDetails.data.senderId.name))
            }
        }
        return Promise.all(results);
    }
    
    function localSmsPromisified(message = "", contact = "", name = "") {
        return new Promise(function (resolve, reject) {
            return localSms(message, contact, name, function (response) {
                if (response.orginalResponse.Data.hasOwnProperty("id")) {
                    return resolve({ alreadySent: 1 });
                } else {
                    var smsLength = generalHelper.calculateSmsLength(response.message);
                    var sentItemsData = {
                        user_id: smsDetails.data.user_id,
                        number: response.contact,
                        message: response.message,
                        length: smsLength,
                        status: 0,
                        type: 'portal',
                        date: moment().format("YYYY-MM-DD HH:ss:mm")
                    };
                    return resolve(sentItemsData);
                }
            });
        });
    }
    
    

    Refer this article to understand the concepts

    【讨论】:

      猜你喜欢
      • 2019-05-16
      • 1970-01-01
      • 2018-03-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多