异步并行函数

时间:2017-03-11 11:02:18

标签: javascript mysql node.js asynchronous

我想连续运行一堆mysql个查询。但是它需要等到最后一次查询执行并且我正在使用async。所以我试图用我需要的函数填充我的数组,然后在async.parallel body中运行这些函数。

甚至可以做那样的事情吗?

function getAllInstructions(req,res){
    let mysqlQuery = `SELECT instruction.id,instruction.title,instruction.description
                      FROM users_instrusctions join instruction on users_instrusctions.instruction_id = instruction.id
                                               join user on users_instrusctions.user_id = user.id
                      WHERE user.id = '` + req.params.id + "';";
    connection.query(mysqlQuery, function(err, rows, fields) {
    if (!err) {
        var instructions = new Array();

        for (var i=0; i<rows.length;i++) {
             var instruction = new Instruction();
             instruction.id = rows[i].id;
             instruction.title = rows[i].title;
             instruction.description = rows[i].description;
             instructions.push(instruction);
        }

        functionsToDo = [];
        for (var i=0;i<instructions.length;i++) {
            functionsToDo.push(getStepsForInstruction(instructions[i].id));
        }
        //here i need to execute queries
        async.parallel(functionsToDo, function (err, result) {
            res.json(instructions); 
        });
    }
    else {
        console.log('Error while performing Query.');
    } 
});}

0 个答案:

没有答案