Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

350
Visualizações
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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda