0

From trawling through answers I realize this question gets asked a lot. For anyone like me that is new to this I recommend reading https://dev.to/ramonak/javascript-how-to-access-the-return-value-of-a-promise-object-1bck

I also read this: Async function returning promise, instead of value

Nevertheless despite all this I still can't get my code to work!

/* GET users listing. */
router.get('/', function(req, res, next) {
  const message = test()
  res.render('index', { title: ' Test: ' + test() });
});

function test()
{
  const url = 'http://backend:5000/api'
  fetch(url).then(response => {
    console.log("This is working")
    return response.text();})
    .catch(error => {
      console.log("This is not returning anything")
      return "Error"});
}

This gives me a response of Test: undefined. The console.log shows is printing "This is working" and I have separately tested the API that it is calling previously (it's a simple one I created in Flask that returns some text).

zidniryi
  • 1,212
  • 3
  • 15
  • 36
Badrul
  • 179
  • 10
  • 2
    You are not `await`ing `test()`, and also you are returning `response.text()` to nowhere, since you don't return the promise returned by `fetch(...).then(...)` from `test`. (And for `await`ing `test()`, your route handler needs to be `async` and you need to handle your errors.) The code would be clearer if you'd make `test` itself also `async` and replaced `then` with `await`. – CherryDT Mar 23 '23 at 08:06
  • This might have to do with the fact that `response.text()` itself is also a promise. Assign the value to to a variable (or let) that exists outside of the `.then()` scope, kinda like your `const url` then return that value. – Apodemus Mar 23 '23 at 08:10
  • 2
    @Apodemus — No! Absolutely not! That will not work. You’ll return the value of said variable **before** the then callback has assigned a new value to it. – Quentin Mar 23 '23 at 08:12
  • 1
    @Apodemus Review [Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference](https://stackoverflow.com/q/23667086) – VLAZ Mar 23 '23 at 08:17
  • oh that is true, I didn't consider that. Yeah,. the await is the right solution. – Apodemus Mar 23 '23 at 08:20
  • Ok thanks will go through these comments and try to get my my head around it. I had tried async before and ran into issues there. Btw the duplicate question link above points to a terrible answer imo. It's a very lengthy answer that is not newbie friendly and talks about .ajax which I thought was no longer used, – Badrul Mar 23 '23 at 09:13
  • Ps. @CherryDT This was my prior attempt https://stackoverflow.com/questions/75817913/why-do-i-still-keep-geting-object-promise-instead-of-the-actual-text But let me have another go at trying async. – Badrul Mar 23 '23 at 11:09
  • Ok Chat GPT helped me figure it out. It even fixed my code for me! The comments it gave were: a) The test() function is asynchronous and returns a promise, but it is being called without using await or .then() to handle the promise resolution. b) The test() function is not returning anything to the router.get() function, so message will be undefined. c) The resp variable is not being declared with let or const, which can cause issues with variable scope. – Badrul Mar 23 '23 at 11:19

0 Answers0