0

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"]);
})();
Sebastian Simon
  • 18,263
  • 7
  • 55
  • 75
JohnyClash
  • 630
  • 1
  • 5
  • 10
  • 2
    `await go.a` is not the same as `(await go).a`. You need to specify which thing you're awaiting, if you don't want to await on whatever the entire expression evaluates to. – user229044 Oct 14 '21 at 01:14
  • @meagar This solved my problem. Thank you. Although this syntax does seem inconsistent to me. As when I console.log the awaited object it returns an object not a promise. Does the interpreter see 'Promise{}.a'? – JohnyClash Oct 14 '21 at 01:44
  • Yes. If you take `a.b.c` and prepend `await` to it, you've produced `await(a.b.c)` not `await(a).b.c`. As you say, your code was effectively the same as `await Promise{}.a`, when you needed `await(Promise{}).a` – user229044 Oct 14 '21 at 01:51

1 Answers1

1

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']);
  })();
});
CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
  • I simplified the problem to make the issue clear in question, In my actual use case needs to be an async function. When a console.log the async object is return [object Object]. This issue does seems inconsistent, even though you have shown a work around. – JohnyClash Oct 14 '21 at 01:47
  • Then use `.then` on the Promise to wait for it to resolve. – CertainPerformance Oct 14 '21 at 01:48