Search code examples
node.jsnodejs-streamnodejs-server

How to get to be return and print in outside function using node js


I tried one simple function in node js and pass data to one file on to another file.but its throwing undefined.how to solve it in node js

fun.js

function Get_Value()
{
    client.get('products', function(err,results) {
        return results

    })
    console.log(results)
}

I tried to print value in outside the function but its print undefined how to print result in out side function


Solution

  • async function Get_Value() {
      let results = await new Promise(function(resolve) {
        client.get("products", function(err, results) {
          resolve(results);
        });
      });
    
      console.log(results);
    }