0

I am retrieving data from mysql database using Nodejs. Previously, I used to use mysql package but now I am using mysql2 package since it has promises integrated. Now, the question is if I have to run multiple tasks which depend on the output of previous task, what can I use instead of async.waterfall(tasks, callback)? Also, I would like to know if I need to implement promise keyword if it is already in mysql2?

example code:

async function(req, res){
var dbquery = "select ....."

    classicasync.waterfall([
       function(callback){
         db.getconnection(function(err, connection){
           (async()=>{
              db.query = util.promisify(db.query);
               let product = await db.query(dbquery)

          })
        callback(null, 'Task1', 'Task 2');
       },
      function(arg1, arg2, callback){
         ........
        callback(null, arg3)
    },
     function(arg1, callback){
       arg1 += 'completed';
       callback(null, arg1);
    }
    ], function(err, result){
         console.log(result);
    });

This is a huge function, so I couldn't write all of it.

Thank you for your help.

shumana chowdhury
  • 1,742
  • 2
  • 14
  • 34
  • You'd just use regular code with `await`s..? See https://github.com/sidorares/node-mysql2/blob/master/documentation/Promise-Wrapper.md#es7-async-await – AKX Dec 21 '22 at 08:07
  • Thank you for your answer. I have a very basic question. Sorry for asking that. My question is if I use. async function example2 () { const mysql = require('mysql2/promise'); const pool = mysql.createPool({database: test}); // execute in parallel, next console.log in 3 seconds await Promise.all([pool.query('select sleep(2)'), pool.query('select sleep(3)')]); console.log('3 seconds after'); await pool.end(); } promise in the connection, don't I need to use promises for my other functions in controller? – shumana chowdhury Dec 21 '22 at 08:11
  • Yes, all of your functions will need to be `async` (or use `.then()`, but that's pretty ugly). – AKX Dec 21 '22 at 09:24

0 Answers0