2

I have this function that I want to use to assign the value of a user object to a variable so that it can be used multiple times.

function getUser() {
    if (req.cookies.token) {
      authService().getUser()
        .then((user) => {
          if (user) return user;
          return null;
        }).catch((error) => {
        console.log(error);
      });
    }
  }

When the user object is logged to the console inside the .then() statement, it works correctly. However then I call the function, it returns undefined.

console.log('Get user: ', getUser());

Is there a correct way to return the result of a promise from a function?

Tom
  • 2,543
  • 3
  • 21
  • 42

0 Answers0