Sunday 15 May 2011

javascript - console.log() async or sync? -



javascript - console.log() async or sync? -

i reading async javascript trevor burnham. has been great book far.

he talks snippet , console.log beingness 'async' in safari , chrome console. unfortunately can't replicate this. here code:

var obj = {}; console.log(obj); obj.foo = 'bar'; // outcome: object{}; 'bar'; // book outcome: {foo:bar};

if async, anticipate outcome books outcome. console.log() set in event queue until code executed, ran , have bar property.

it appears though running synchronously.

am running code wrong? console.log async?

console.log not standardized, behavior rather undefined, , can changed release release of developer tools. book outdated, might reply soon.

to our code, not create difference whether console.log async or not, not provide kind of callback or so; , values pass referenced , computed @ time phone call function.

we don't know happens (ok, could, since firebug, chrome devtools , opera dragonfly open source). console need store logged values somewhere, , display them on screen. rendering happen asynchronously sure (being throttled rate-limit updates), future interactions logged objects in console (like expanding object properties).

so console might either clone (serialize) mutable objects did log, or store references them. first 1 doesn't work deep objects. also, @ to the lowest degree initial rendering in console show "current" state of object, i.e. 1 when got logged - in illustration see object {}.

however, when expand object inspect properties further, console have stored reference object , properties, , displaying them show current (already mutated) state. if click on +, should able see bar property in example.

here's screenshot posted in bug report explain "fix":

so, values might referenced long after have been logged, , evaluation of these rather lazy ("when needed"). famous illustration of discrepancy handled in question is chrome's javascript console lazy evaluating arrays? workaround create sure log serialized snapshots of objects always, e.g. doing console.log(json.stringify(obj)). work non-circular , rather little objects only, though. see console.log object @ current state.

javascript asynchronous

No comments:

Post a Comment