0

I'm using asynchronous functions with Promise in order to make use of Google Sheets API. Everything is working fine except for one thing: I'm not able to access the value returned by the API outside of 'then'. Inside then, 'content' is printing the right value. Here is the relevant part of the code:

var val = selectPayloadContent(serviceName)
  .then(content => console.log("Outputting content: ", content))
  .catch(error => console.log(error));

console.log("Outputting content outside: ", val);

val.then(function(result) {
  console.log("Val: ", result);
});

I realize this is a common issue and I've gone through a lot of questions and different articles. However, I can't seem to resolve this problem. Please note:

  • 'val' in the console log is showing Promise {pending}.
    PromiseState is fulfilled and PromiseResult is undefined.
  • 'result' in the console log is showing undefined

Any kind of help will be really appreciated. Thanks.

Joseph
  • 117,725
  • 30
  • 181
  • 234
Khursand Shakeel
  • 181
  • 1
  • 14
  • 1
    `result` is `undefined` because of promise chaining. The callback of the `.then()` callback that creates the `val` promise does log the content, then returns `undefined`, which is the value that the promise is resolved with. – Bergi Oct 28 '21 at 21:40
  • I think you shouldnt use variables to store promise results. You can chain two "then" methods instead. – Abdulhakim Oct 29 '21 at 10:59
  • @Bergi Okay, I understand that. I can simply remove the 'val.then' statement. However, I still don't know how to get 'content' OUT of the upper then block so that I can return it from the main selectPayloadContent function. Having the option to do whatever I want with the value inside then block is unfortunately not enough. – Khursand Shakeel Nov 01 '21 at 10:49
  • @Abdulhakim Sorry I didn't quite get that. Could you please elaborate on that a little? – Khursand Shakeel Nov 01 '21 at 10:49
  • 1
    @KhursandShakeel It's fundamentally impossible to get an asynchronous value immediately. You can only ever access it inside a `.then()` handler or after an `await`. Why do you think is "*having the option to do whatever I want with the value inside then block […] unfortunately not enough*"? – Bergi Nov 01 '21 at 18:12
  • @Bergi Yes you're right, I too have found out from another SO answer that it's not possible. I am creating a custom function to be included in a bot script and I only wanted this specific approach to work due to the bot's limited customization options. Anyway, I have now figured out another approach and it seems to be working. So it's all good now. Thank you for the help! :) – Khursand Shakeel Nov 01 '21 at 21:11
  • If you want to create two actions (one action following another action) you can use promises or async javascript. This is especially useful when your actions involve data fetching or other time taking operations. In short, you can try to rewrite your code block in async-await. – Abdulhakim Nov 02 '21 at 14:07

0 Answers0