0

I have normal function call in server file.

let Val = fileName.load();

In Another file:

 const load = async function(){
  getVal = await getall();
  return getVal
 }

But I'm getting the response like,

 Promise{
  [{Values: valexampl}]
  }

Why my response is getting attached with Promise here. Kindly help me to understand

Priyanka
  • 117
  • 1
  • 5
  • 19
  • Also relevant: [How do I return the response from an asynchronous call?](https://stackoverflow.com/q/14220321) [Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference](https://stackoverflow.com/q/23667086) – VLAZ Oct 21 '20 at 10:33

1 Answers1

1

The function with async keyword in front of function definition returns the Promise type value.

So to get the value, you need to use await keyword like

let Val = await fileName.load();
Derek Wang
  • 10,098
  • 4
  • 18
  • 39
  • 1
    The second piece of code is [pretty useless](https://stackoverflow.com/questions/23667086/why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchron) – VLAZ Oct 21 '20 at 10:34
  • @Derek I cannot use await in the initial function. Even if I remove async also I'm getting the same response. Is there any other way? Please help. – Priyanka Oct 21 '20 at 10:50
  • 1
    Then you need to use like this. fileName.load().then((res) => { let Val = res; }) and inside `then` callback, do the rest. – Derek Wang Oct 21 '20 at 10:52
  • @Derek.W thank you so much – Priyanka Oct 21 '20 at 11:03