0
function test() {

  return new Promise((resolve, reject) => {
    resolve('Yay');
  });

}

function func() {
 return test()
   .then((val) => {  

     return val; 
   })
   .catch((error) => { 
     console.log('handle reject');
   });
}

console.log(func());

I am looking for a way to get the function to return the value from the function call. Right now when we return, we actually return a promise. Whats the ideal way to go about this?

Gayan Hewa
  • 2,277
  • 4
  • 22
  • 41

1 Answers1

0

You can access the value inside a .then()

function func() {
 return test()
   .then((val) => {  

     return val; 
   })
   .catch((error) => { 
     console.log('handle reject');
   });
}

func().then(val){
  console.log(val);
}

since func() is an asynchronous call, your console.log() won't wait for it to finish , you would need to print it in a callback or in a then()

marvel308
  • 10,288
  • 1
  • 21
  • 32