Node.js async issue cross modules

Hi

I am trying to use the async code as shown in the Node.js course.
I am exporting the async function (below).
The function is querying form a postgres SQL.
When I console log the result inside the function, I get correct data.
When I query the return of the result I get undefined.
I need to use the return data in the rest of the program
It looks like I get the print of the return before the function ends.
How can I use the return data in the rest of the program?

The function

exports.getData = async function(client,table){
     await client.query(`SELECT * FROM ${table} `
          , (err, result)=>{
            if (err) throw err;
            console.log(result.rows); // Here I see the result 
            return  result.rows;
    });
}; 

// **The call to the function**
console.log(await cs.getData(client, user)); //Here I get undefine

Hi Guy,

Note first that your function is concatenating strings - that’s not a safe thing to do in an SQL query. I’m not sure which database driver you’re using but you should look for “using bind variables” in the driver’s documentation to fix it.

As for the async problem it looks like your function is simply missing a “return” statement
unlike ruby or perl, in JavaScript a function without return will return the value “undefined”. I can see you have a return statement in the inner callback function, but you need to write “return” before the await call in order to make the function return a promise.

Cheers,
ynon