0

I have this code that has 3 sql query and runs one by one. The running time of the query is too much so i was thinking that is there any way I can run all these query in parallel and return the result at last after all the queries has stopped finishing. Here is the code:

async function getChartData (req, res){
    try{

        var sqlquery=" " 
        var sqlsecond=" "
        var sqlthird=" "

        let result1 = await selectquery(sqlquery)

        let result2 = await selectquery(sqlsecond)

        let result3 = await selectquery(sqlthird)

        return res.json({result1:result1, result2:result2, result3:result3});

    }
    catch(err){
        // response.status(500).end();
        console.log(err);
    }
  }


async function selectquery(sqlquery){
    return new Promise((resolve, reject) => {
        mysqlConnection.query(sqlquery,(err,result)=>{
            if(err){
                reject(err);
            }
            else{
                resolve(result);
            }
        });
    });
}
mysqlConnection.end();
varun chauhan
  • 89
  • 1
  • 11

1 Answers1

0

This should work:

const sqls = [sqlquery, sqlsecond, sqlthird]; 
const [result1, result2, result3] = await Promise.all(sqls);
mindriven
  • 143
  • 1
  • 8