简体   繁体   English

使用asise.each和promises - async,每个都不会停止

[英]Using async.each with promises - async each never stops

I am building this function in node that is a mix of promises querying a DB and a async each. 我在节点中构建此函数,该节点是查询DB和异步的promises的混合。 The issue is that the async.each function never stops. 问题是async.each函数永远不会停止。 The promise2 function is never triggered. promise2函数永远不会被触发。 Is it an issue in my code or cant an async.each function be mixed with promises? 这是我的代码中的问题还是不能将async.each函数与promises混合使用? Is there a better way to code what I want to do? 有没有更好的方法来编码我想要做的事情?

Thanks a lot for your help 非常感谢你的帮助

promise1().then(function(arr1){
  async.each(arr1, function(obj1, callback) {
    build_select_request(obj1).then(function(select_request){
      query_database(select_request).then(function(result){
        if (result){                       
          build_update_request(obj1).then(function(update_request){                        
            do_query(update_request).then(function(result){                                          
              callback(null)
            }, function(error){
              callback(error)
            })
          }, function(error){
            callback(error)
          })
        } else {
          build_insert_request(obj1).then(function(insert_request){            
            do_query(insert_request).then(function(result){                                                                      
              callback(null)
            }, function(error){
              callback(error)
            })
          }, function(error){
            callback(error)
          })
        }
      }, function(error){
        callback(error)
      })
    }, function(error){
      callback(error)
    })
  }, function(err) {
    // if any of the file processing produced an error, err would equal that error
    if (err) {
      // One of the iterations produced an error.
      // All processing will now stop.
      reject(err)
    } else {
      promise2().then(function(success){        
        resolve(success)
      }, function(error){        
        reject(error)
      })
    }
  })
}, function(error){  
  reject(error)
})

Is it an issue in my code or cant an async.each function be mixed with promises? 这是我的代码中的问题还是不能将async.each函数与promises混合使用?

It can, but it should not. 它可以,但它不应该。 Writing promise code and then falling back to callbacks results in, well, what you currently have. 编写承诺代码然后回退到回调会导致你现在拥有的东西。

Instead, make sure to use proper promise chaining: 相反,请确保使用正确的承诺链:

var promise = build_select_request(obj1).then(function(select_request){
  return query_database(select_request).then(function(result){
    if (result){                       
      return build_update_request(obj1).then(do_query);
      /* .then(function(update_request){                        
        return do_query(update_request);
      }) */
    } else {
      return build_insert_request(obj1).then(do_query);
      /* .then(function(insert_request){            
        do_query(insert_request)
      }) */
    }
  })
})

You now can use that with async.each like this: 您现在可以使用async.each如下所示:

async.each(arr1, function(obj1, callback) {
  var promise = …;
  promise.then(function(result) {      
    callback(null, result)
  }, function(error) {
    callback(error);
  });
}, function(err) {
  …
})

but you better avoid this and instead use Promise.all which also lets you avoid the Promise constructor antipattern that you fell for (given the resolve / reject calls in your outermost callbacks): 但你最好避免这种情况,而是使用Promise.all ,这也可以让你避免使用的Promise构造函数反模式 (给出最外层回调中的resolve / reject调用):

promise1().then(function(arr1) {
  return Promise.all(arr1.map(function(obj1) {
    var promise = …;
    return promise;
  }));
}).then(function(results) {
  …
}, function(err) {
  …
});

I was missing "return" 我错过了“回归”

promise1().then(function(arr1){
  async.each(arr1, function(obj1, callback) {
    build_select_request(obj1).then(function(select_request){
      return query_database(select_request).then(function(result){
        if (result){                       
          return build_update_request(obj1).then(function(update_request){                        
            return do_query(update_request).then(function(result){                                          
              callback(null)
            }, function(error){
              callback(error)
            })
          }, function(error){
            callback(error)
          })
        } else {
          return build_insert_request(obj1).then(function(insert_request){            
            return do_query(insert_request).then(function(result){                                                                      
              callback(null)
            }, function(error){
              callback(error)
            })
          }, function(error){
            callback(error)
          })
        }
      }, function(error){
        callback(error)
      })
    }, function(error){
      callback(error)
    })
  }, function(err) {
    // if any of the file processing produced an error, err would equal that error
    if (err) {
      // One of the iterations produced an error.
      // All processing will now stop.
      reject(err)
    } else {
      promise2().then(function(success){        
        resolve(success)
      }, function(error){        
        reject(error)
      })
    }
  })
}, function(error){  
  reject(error)
})

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

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