Hi,
Following the lesson on Express.js of the Full-Stack Engineer course, I have written the following file:
On the other hand, in order to check that this “server” is working properly, I have tested it with Postman and, besides, following the approach followed at the time of building other APIs (e.g., the Spotify api of the React Module) I have written the following function to fetch the data from the “server”:
With Postman I get the expected result (the db), so the “server” seems to be working properly. However, whenever I try to assign the returned value of the function to a variable [e.g. let y = x()], I do not get the expected result (“Promise: {Pending}” reads the log of the variable).
I have been searching for an answer to this and checked that, because of the asynchronous nature of the function, this results in a promise and, therefore, if I add a “.then” block, I can log the expected result [e.g. x().then(result => console.log(result))]; however, following this solution, as far as I have tested, I cannot assign the result of the promise to a variable outside the “.then” block.
I do not understand why, e.g., in the Spotify Api or in other Apis built during the course I have been able to assign the value returned in an asynchronous function to a variable and then work with that variable, and in this case I cannot because I get the “Promise: {Pending}” result.
Kind regards and thanks in advance for your help and answers.