简体   繁体   English

在Node.js中使用Q.js Promise进行循环的正确方法

[英]Correct way to make loop with Q.js promise in nodejs

I hope someone can help me better understand the promises. 我希望有人能帮助我更好地理解诺言。 I'm pretty rookie with them. 我和他们很漂亮。 I'm doing a DB query with promises, when the result is correct get an array of data. 我正在做一个带有承诺的数据库查询,当结果正确时,获取一个数据数组。 Then I need to make a loop iterating this array and perform for each element delete operation and after that delete operation to another operation of sending, when both operations finished, then, repeat the process for each element of the array, but must be completed for each element the finish operation and send operation before starting another array element ... with my code that does not happen, the loop make delete operation for each element without awaiting the delete and send operation return result.... Both operation delete and shipping have the structure 然后,我需要循环遍历此数组,并为每个元素删除操作执行该操作,然后在删除操作之后执行另一个发送操作,当两个操作都完成后,然后对数组的每个元素重复该过程,但必须完成该操作每个元素先完成操作并发送操作,然后再开始另一个数组元素...对于未发生我的代码,循环对每个元素进行删除操作,而无需等待删除和发送操作返回结果。...操作删除和装运有结构

function x (makeQuery){
  .......
  var deferred = Q.defer();
   .....
   function(err, result) {
                    if (err) {
                      deferred.reject(err);
                    } else {
                     deferred.resolve(result);
                    }

                });
  return deferred.promise;
}

And my code is : 我的代码是:

 getElementInPostgres(makeQuery)
        .then(function (obj) {
          if (...){
                results= obj['rows']
                index =0
                for (var i = 0; i < results.length; i++) {
                    var notification = {}
                  notification.tag='SubscriptionNode'
                  notification.indexSocket=results[i]['indexsocket']
                  notification.clientID=results[i]['clientid']
                  notification.callbackURL=results[i]['callbackurl']
                  notification.deploymentID=results[i]['deploymentid']
                  notification.clusterID=results[i]['clusterid']
                  notification.date=results[i]['registered']
                  var query = "DELETE FROM.....";
                  var params = [notification.indexSocket];
                  deleteInPostgres(query,params,notification)
                  .then(function (notification) {
                      if(notification.clientID){
                          sendInfoToPython(notification)
                           .then(function (obj) {
                               ..........
                           }).fail(function (err) {
                              ......
                           });
                      }

                  }).fail(function (err) {
                    ......
                   });
                 }
               }
               else{
                .........
               }
              }).fail(function (err) {

                });

You can continue to use Q but for iterating the first result you can use async.each ( https://github.com/caolan/async#each ) . 您可以继续使用Q,但是要迭代第一个结果,可以使用async.each( https://github.com/caolan/async#each )。 In the iteratee you can perform the delete task. 在iteratee中,您可以执行删除任务。 In the callback of the iteration resume your operation of sending. 在迭代的回调中,恢复发送操作。 It will make your code simpler and less verbose 它将使您的代码更简单,更省力

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

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