简体   繁体   English

从 async.each 调用回调 function 并处理结果

[英]Calling a callback function from async.each and processing the results

I'm just starting to work with Javascript and Node, and Async and callbacks concepts are not something I have under control right now.我刚刚开始使用 Javascript 和 Node,而异步和回调概念现在不是我能控制的。

I have to call a function for each element of a documents Array.我必须为文档数组的每个元素调用 function。 This function will call to DB and get me an array of the document annotations.这个 function 将调用 DB 并给我一个文档注释数组。 I want to get all the annotations and put them on the same array.我想获取所有注释并将它们放在同一个数组上。 Something similar to this:与此类似的东西:

     //function in an async waterfall
      function(docs,callback){
        let annotationsArray = [];
        async.each(docs, (doc, callback2) => {
          getAnnotationsFromDocument(doc.Id, callback2);
        }, function (err,annotations){
          if (err){
            callback(err);
          } 
          annotationsArray = annotationsArray.concat(annotations);
          callback(null, annotationsArray);
        });
        
      },
//Next waterfall function

About the getAnnotationsFromDocument function, this is a simplified structure of it:关于getAnnotationsFromDocument function,这是它的简化结构:

function getAnnotationsFromDocument(docId,callback){
  
initDB();

  var async = require('async');
  async.waterfall([
    function authorize(callback){
      //checkAuthorization
(...)
    },
    function getRfpdocAnnotations(auth, metadata, callback){
      //call to DB
(...)
    },    
    function processRfpdocAnnotations(rfpDocAnnotations,metadata,callback){
 (...)
      callback(null,annotationsList);
    }
  ], function (err, result) {
    if(err) {
      callback(err);
    } else {
      callback(null, result);
    }       
  });   
}

Unfortunately, I'm unable to code it properly.不幸的是,我无法正确编码。 I'm unable to get the results from the function before exiting the async.each.在退出 async.each 之前,我无法从 function 获得结果。 Could somebody explain me how to structurate the code for this?有人可以解释一下如何为此构建代码吗?

Debugging I've found that the function getAnnotationsFromDocument gets the data and execute the last callback(null, result);调试发现 function getAnnotationsFromDocument 获取数据并执行最后一个回调(null, result); properly, but when I get to function (err,annotations){ , annotations is undefined.正确,但是当我到达function (err,annotations){时,注释是未定义的。

Ok, I think I got it:好的,我想我明白了:

  • First problem was that async.each doesn't return the results on the callback like I was expecting.第一个问题是 async.each 没有像我预期的那样在回调中返回结果。 Unlike waterfall, it just returns the errors.与瀑布不同,它只返回错误。 I should have payed more attention reading the documentation.我应该更加注意阅读文档。
  • Secondly, I had to create a callback on the getAnnotationsFromDocument call to process the results.其次,我必须在 getAnnotationsFromDocument 调用上创建一个回调来处理结果。
  • And finally, I was not executing the call to the callback of async.each, so the execution didn't get to the async.each callback and didn't continue to the next async.waterfall function.最后,我没有执行对 async.each 回调的调用,所以执行没有到达 async.each 回调,也没有继续到下一个 async.waterfall function。

To be quite honest, I'm not sure it's a correct answer, but it does what I was trying to achieve.老实说,我不确定这是一个正确的答案,但它确实实现了我想要实现的目标。

  // function part of an async.waterfall
  function(docs,callback){
    let annotationsArray = [];
    async.each(docs, (doc,callback2) => {    
      getAnnotationsFromDocument(doc._id, function(err,result){
          if (err){
            callback2(err);
          }else{
            annotationsArray = annotationsArray.concat(result);
          }
          callback2();
        })
    }, (err) =>{
      if( err ) {
        callback(err);
      } else {
        callback(null,annotationsArray); //to the next waterfall function
      }
    });

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

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