簡體   English   中英

NodeJS的承諾,遞歸,異步續

[英]NodeJS Promises, Recursion, Asynchronous Continued

@lyjackal在這里幫助我(NodeJS 異步和遞歸 )解決了尋找孩子的遞歸問題。

我仍在努力弄清nodejs的承諾。 我的問題與上一篇文章類似,但略有不同。 我覺得這段代碼很接近,但是沒有用。

我有一個分配,並且分配具有對路徑的引用。 該路徑以及所有路徑可能有也可能沒有父路徑。 我需要此代碼,以便按原樣查找路徑的路徑,並將每個路徑的一些屬性堆疊在數組中。 最后的數組如下所示:

[ [0]=>{field: name, match: match_type, value:value},
  [1]=>{field: name, match: match_type, value:value},
  ...]

數組的順序甚至無關緊要。 這是帶有Promises ...的代碼,該代碼已損壞:

exports.assignRecursiveQuery = function(req,res) {
    var methods = {}
    var query_to_save = []

    methods.recursiveParents = function(path) {
        return new Promise(function(resolve, reject) {
          Path.find({
            _id: path.parent
          }).exec(function(err, parentPaths) {
            Promise
              .all(parentPaths.map(function(parentPath) {
                return methods.recursiveParents(parentPath) 
              }))
              .then(function(promisedPaths) {
                return resolve(promisedPaths);
              });
          });
        });
      }

    Path.find({_id: req.assignment.path_end}).exec(function(err,lastPaths) {
        //add first query rule
        console.log(lastPaths[0]);
        query_to_save.push({field:lastPaths[0].field.api,match:lastPaths[0].match,value:lastPaths[0].value})

        Promise.all(lastPaths.map(function(path) {
          return methods.recursiveParents(path);
        })).then(function(resolvedPaths) {
            for( var x=0; x<resolvedPaths.length; x++ ) {
                console.log(resolvedPaths[x])
                query_to_save.push({field:resolvedPaths[x].field.api,match:resolvedPaths[x].match,value:resolvedPaths[x].value});
            }
            console.log(query_to_save);
            res.jsonp(req.assignment)
        });
    });
}

我可以使用以下代碼按需運行它,不確定是否是“正確的方式”,但它確實按預期運行。

 exports.assignRecursiveQuery = function(req, res, next) { var query_to_save = [] var methods = {}; methods.recursiveChildren = function(path) { var item_to_save = { field: path.field.api, match: path.match, value: path.value } query_to_save.push(item_to_save); return new Promise(function(resolve, reject) { Path.find({_id: path.parent}).exec(function(err, parentPath) { Promise .all(parentPath.map(function(parent) { /* collect a promise for each child path this returns a promise */ return methods.recursiveChildren(parent); })) .then(function(resolvedPaths) { /* the top level promise */ resolve(); }); }); }); } Path.find({_id:req.assignment.path_end}).exec(function(err, paths) { Promise.all(paths.map(function(path) { return methods.recursiveChildren(path); })).then(function(resolvedPaths) { var assignment = req.assignment; assignment.assign_query = query_to_save; assignment.save(function(err) { if (err) { return res.status(400).send({ message: errorHandler.getErrorMessage(err) }); } else { req.assignment = assignment; next(); } }); }); }); }; 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM