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

264
Visualizações
Why does property access with await appear to result in `undefined` when the object is returned from an async function?

Please run the snippet and observe the output code to understand my issue. I stripped the problem down and made the console.logs easy to understand.

It seems that the suggested syntax for accessing object properties works in synchronous functions but does not work in an asynchronous function.

The suggested syntax to access object properties does not to work inside of async functions. Is there a logical reason for this difference in behavior, have I overlooked something in my syntax, or is this an inconsistency in JavaScript’s async objects.

var goo = (function() {
  let a = "as";
  let b = "boot";
  
  return {
    a,
    b
  }
})();

(function() {
  console.log("console.log 1:", goo)
  console.log("console.log 1.5:", goo.a);
  console.log("console.log 1.75:", goo["a"]);
})();

var go = (async function() {
  let a = "asile";
  let b = "beet";
  return {
    a,
    b
  }
})();

(async function() {
  console.log("console.log 2:", await go);
  console.log("console.log 2.5:", await go.a);
  console.log("console.log 2.75:", await go["a"]);
})();

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Your goo evaluates to a plain object, but your go evaluates to a Promise:

var go = ( async function(){
    let a = 'asile';
    let b = 'beet';
    return {a:a,b:b}
})();

So go doesn't have properties of a and b - it's not the object that the Promise resolves to, it's only the Promise. You need to either

  • extract the resolve value from the Promise before working with it, or
  • remove the async from go's IIFE so that it returns the object, and not a promise that resolves to the object.

var go = ( function(){
    let a = 'asile';
    let b = 'beet';
    return {a:a,b:b}
})();

(async function(){
    console.log('console.log 2:'+  await go);
    console.log('console.log 2.5: ' + await go.a);
    console.log('console.log 2.75:'+  await go['a']);
})();

var goProm = (async function(){
    let a = 'asile';
    let b = 'beet';
    return {a:a,b:b}
})();

goProm.then((go) => {
  (async function(){
      console.log('console.log 2:'+  await go);
      console.log('console.log 2.5: ' + await go.a);
      console.log('console.log 2.75:'+  await go['a']);
  })();
});

about 4 years ago · Juan Pablo Isaza Relatório
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