0

router.js
I have a function named controller, this is being called here -

let data = controller();

controller.js

function controller() {

//sendData below is a promise defined in another file, which is reading content of file and sending the data
sendData().then((data) => {
   //perform some action on data
   console.log(data)
   return data;

   }).catch((err) => {
      return err;
   })
}

The data being returned from the controller function is undefined, though i see data to be present in the success callback function on doing console.log

How to correctly return the data from the controller function? Thanks in advance.

  • when you are assigning a variable to a function your function should return a value. Try adding a return in your controller function. – Anup May 01 '21 at 11:17
  • while returning from the controller function, undefined gets returned due to the async behavior – Shubham May 01 '21 at 11:20

0 Answers0