0

i am very novice to nodejs, i am using generic-pool with mariasql. everything works fine. But i moved my code inside a function like this and i am not quiet sure what is the nodejs way of event handling so that i can actually get the results from the function back

var pooledQuery = function (query) {
    var result;
    //return here will be the return value of pool.acquire
    pool.acquire(function(err, client) {
        var db;
        if (err != null) {
            console.log(err)
            return err;
        }

        result = [];
        db = client.query(query);
        db.on('result', function(res) {
            return res.on('row', function(row) {
                console.log(row);
                return result.push(row);
            }).on('error', function(err) {
                console.log('error');
            }).on('end', function(info) {
                return result;
            });
        }).on('end', function() {
            pool.release(client);
            return result; <-- i want to know how i can obtain this return value outside of this function?? 
        });
    });
    //since pool.acquire is non-blocking the code will reach here and return an empty value
    return result;
};

what is the nodejs way of being able to accomplish something like this using the function above

var result = pooledQuery("SELECT foo, bar FROM foobar");

i am using this generic-pool from nodejs https://github.com/coopernurse/node-pool

DevZer0
  • 13,433
  • 7
  • 27
  • 51
  • 1
    A good summary of options is discussed in "[How to return the response from an asynchronous call?](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call)" – Jonathan Lonowski Aug 07 '15 at 03:02

1 Answers1

2

Return a promise

   var pooledQuery = function(query) {
       return new Promise(function(resolve, reject) {
           //return here will be the return value of pool.acquire
           pool.acquire(function(err, client) {
             var db;
             if (err != null) {
               reject(err); // oh no! error, let the promise know that it failed
             }

             result = [];
             db = client.query(query);
             db.on('result', function(res) {
               return res.on('row', function(row) {
                 console.log(row);
                 return result.push(row);
               }).on('error', function(err) {
                 reject(err) // here too
               }).on('end', function(info) {
                 return result;
               });
             }).on('end', function() {
               pool.release(client);
               resolve(result); // your promise is resolved, 
               // any ".then" callback applied to it will be called now. 
               // i.e `pooledQuery(query).then(function(result){console.log(result)})
             });
           });
         }
       };

use like so

pooledQuery("SELECT foo, bar FROM foobar").then(function(result){console.log(result);})

There are many promise libraries. Examples using bluebird

lyjackal
  • 3,984
  • 1
  • 12
  • 25