0

I tried two function in(async function,normal function)in node js.normal function its sucessfully return value.but async function its cant return value.How to fix it

normal function

index.js

var sample_data = require('./product')

const data = sample_data
console.log(data)

product.js

function sample()
{
    console.log("hai")
    return "hello"
}

module.exports = sample

async function

index.js

var sample_data = require('./product')

const data = sample_data
console.log(data)

product.js

async function sample()
{
    console.log("hai")
    return "hello"
}

module.exports = sample

normal function

Expected output
hai
hello

async function

Expected ouput
hai
hello

but I got output
[AsyncFunction: sample]

smith hari
  • 437
  • 1
  • 11
  • 22

2 Answers2

1

There are two ways

Using then

sample().then(result => console.log(result));

OR using await to wait and get results till execution of next statement

var result = await sample();
console.log(result);
XCEPTION
  • 1,671
  • 1
  • 18
  • 38
0

async functions wrap the return value in promise so to see results you need .then()

   sample().then(result=>{console.lot(result)});
ruggierom
  • 99
  • 5
  • sample was the name of the function you made, add it where you want to console.log the results of sample() – ruggierom May 09 '19 at 04:37