0

I am facing a weird issue while doing console.log of variable inside an async function. see below code -

var blackbox = async function (){
    return Promise.resolve({a: 1, b: {c:[2,3], d: 4}});
}

var nester = async function (){
    return blackbox().then(res => {console.log(res); console.log(res.b.c);return res;});
}

var wrapper = async function (){
    return nester().then(res => {
        res.b.c = []; return res;
    });
}

wrapper();

I ran this in google chrome and saw the output to be the below screenshot enter image description here

to explain - first console.log should have c NOT as empty. Moreover, even if it was to be empty why is the second log just below it shows c is, in fact, not empty.

nilinswap
  • 1,226
  • 1
  • 10
  • 17
  • Side note: use `async` only if you need to use `await` inside the function (this behaviour is observed without the `async` too). – sp00m Dec 11 '20 at 14:31
  • Other side notes: 1. console.log is not itself a synchronous operation and 2. console.logging a reference type (e.g. object, array) logs a *live view* of the object, not a snapshot in time. – Jared Smith Dec 11 '20 at 14:35
  • See also: [Is Chrome's JavaScript console lazy about evaluating arrays?](https://stackoverflow.com/questions/4057440/is-chromes-javascript-console-lazy-about-evaluating-arrays) – Aplet123 Dec 11 '20 at 14:36

0 Answers0