2016-02-12 19 views
3

过去几天我一直试图完成以下操作,而我无法解决它。我觉得我已经尝试了一切。所以这里...缓存来自异步函数的结果,并将其传递给Async.js中的下一个函数

我的路线被赋予一个JSON对象,其中包含所有信息来创建一个新的测验。 换句话说 - 一个包含有关新测验信息的对象,一系列问题,其中每个问题都包含一组答案。

我使用async.js瀑布功能,并要做到以下几点:

  • 保存新的测验,DB,并通过新的测验ID从数据库返回到下一个瀑布功能

  • 遍历每个问题。缓存从数据库返回的新问题ID,并将它们传递给下一个瀑布函数。这里的问题是缓存ID。由于该功能是异步的,我不能缓存结果的任何地方,以在未来的函数中使用...

  • 循环每一个答案,并将其保存到DB

这是我得赶紧:

router.post('/quiz/create', function (req, res) { 
    // JSON object with the new Quiz 
    var json = req.body; 
    async.waterfall([ 
     function(callback) { 
      // Returns a Quiz ID for the new quiz, and passes it to the next waterfall function 
      db.createQuiz(json, function(err, result) { 
       // ID returned from DB is formatted as [{'': id}], hence result[0][''] 
       callback(null, result[0]['']); 
      }); 
     }, 
     function(quizID, callback) { 
      // Loop through each question in the quiz 
      async.forEachSeries(json.questions, function(question, callback) { 
       // Save the question to DB, and get the new question ID returned 
       db.createQuestion(question, quizID, function(err, result) { 
        // TODO: cache the new question ID's in an array somewhere to be passed to the next waterfall function 
        // Start next iteration of the loop 
        callback(); 
       }); 
      }, function(err) { 
       // Done with all questions. Pass question ID's to next function 
       callback(null, cachedQuestionIDs); 
      }); 
     }, 
     function(cachedQuestionIDs, callback) { 
      // TODO: access the question ID's, and use them to loop through and save answers to DB 
     } 
    ], function(err, result) { 
     res.json({ 
      success: true, 
      message: 'Quiz created!' 
     }); 
    }); 
}); 

回答

2

的值只是存储在async.forEachSeries()以外但在async.waterfall()控制流程的包裹第二函数内的阵列。你可以在你的async.forEachSeries()执行完毕后返回这个数组。

function(quizID, callback) { 
    var cachedQuestionIDs = []; 

    // Loop through each question in the quiz 
    async.forEachSeries(json.questions, function(question, callback) { 
    // Save the question to DB, and get the new question ID returned 
    db.createQuestion(question, quizID, function(err, result) { 
     // Store our result 
     cachedQuestionIDs.push(result); 

     // Start next iteration of the loop 
     return callback(); 
    }); 
    }, function(err) { 
    // Done with all questions. Pass question ID's to next function 
    return callback(null, cachedQuestionIDs); 
    }); 
} 
+0

谢谢:)它的奇怪,因为所有的时候,我曾试图以另一种范围内的ID添加到一个数组,这将永远是'undefined'。 我现在可以在瀑布中的最后一个函数中访问ID。 我会尽量让其余的人明天上班。 虽然我不禁觉得我的方法有点肮脏。 在第二个瀑布函数中嵌套循环会更正确。对于我添加到数据库中的每个问题,我可以遍历所有答案,并将它们循环到同一个函数中的数据库中。 但我不知道如何做到这一点与所有的异步事情。 – Petter

1

我终于得到它正常工作,与一个更优雅的解决方案使用嵌套forEachSeries循环的问题和答案。这里没有错误处理。只是为了展示基本的想法。这是瀑布中的第二个功能。

function(quizID, callback) { 

     async.forEachSeries(json.questions, function(question, callback) { 
      db.registerQuestion(question, quizID, function(err, result) { 
       async.forEachSeries(question.answers, function(answer, callback) { 
        db.registerAnswer(answer, result[0][''], callback); 
       }, function() { 
        callback(); 
       }); 
      }); 
     }, function() { 
      callback(); 
     }); 

} 
相关问题