Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

347
Views
Cannot access the value outside of Promise 'then' method

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.

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!