0

I have trouble getting stringReply value into my app.post(using Express). For what I know I think is that the code has completely executed before the promise is resolved, thus giving me a undefined value when I tried to log stringReply.

fetch(osURL, setting)
.then(loadRes)
.then(logger)
function loadRes (res){
  return res.json();
}
async function logger (reply){
  let stringReply = await reply.answer;
  sendingReply(stringReply);

  return stringReply
}
function sendingReply(stringReply){  
  let response = {
    "fulfillmentMessages": [
      {
        "text": {
          "text": [
            stringReply
          ]
        }
      }
    ]//fulfillmentMessageObj
  };//response3
  console.log(stringReply); //I can still get the value I wanted here
  return response
} 
app.post('/', function( req, res){

console.log(stringReply);
//obtain value here before posting the value to other place
}

  • Does this answer your question? [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – VLAZ Jun 10 '20 at 06:45
  • I think there is a problem with your logger function: The result of sendingReply is not handled. the response object never logged or received. – htho Jun 10 '20 at 07:03
  • Is this all your code? Is the `fetch().then().then()` part of a function, or how is it being called from inside your `app.post()` handler? – Prisoner Jun 10 '20 at 09:20
  • because at the ```app.post``` I received a undefined value when i console.log but when on the ```function sendingReply()``` I managed to receive a value string from the console.log –  Jun 10 '20 at 10:50

1 Answers1

0

The problem isn't about waiting for the Promise - it sounds like you have a variable scoping issue (and, separately, a problem with understanding return values). stringReply is undefined in the app.post() callback because it isn't set there.

You're able to log it in sendingReply(stringReply) because you're passing a parameter named stringReply. Even tho these have the same name, since this is a parameter, the name of that parameter is available inside the function.

In your logger() function, you declare stringReply as a local variable. When you "return" it, you're not returning the variable - you're returning the value of the variable.

There are, however, additional problems with your code.

Since the call to fetch() is outside your app.post() callback function, it gets called when your code is first run, but not when the '/' endpoint is hit. While this is sometimes intentional - it probably isn't in your case.

Furthermore, you're not storing the result from the call to the fetch().then().then() chain. So you probably want that block of code to read something like

const loggerResult = fetch(osURL, setting)
  .then(loadRes)
  .then(logger)

Even if you were storing the result, it probably isn't the result you want. This is because logger() appears to call sendingReply(), but it doesn't do anything with the result either. That function might need to do something with the reply as well. Perhaps something like

async function logger (reply){
  let stringReply = await reply.answer;
  cont ret = sendingReply(stringReply);

  return ret
}

There are other problems with your code that are confusing as well. Why, for example is logger() an async function? And why does it appear to be using await on something that isn't a function? None of that seems necessary in your sample code.

Prisoner
  • 49,922
  • 7
  • 53
  • 105