8

Is it posible to make logging to the console synchronous? I often run into situations where the code execution is faster than dumping the structures. That resolves in outputting already changed objects.

I sure can walk through the code with debugger, make unit tests etc., it's just often convenient to simply console.log stuff just to make a general idea of what is going on.

Mikulas Dite
  • 7,790
  • 9
  • 59
  • 99

3 Answers3

11

You could create a copy of the object before passing it to console.log. Look here for a function to create a deep copy of your object.

Edit:

Now implemented in Chrome, see here

Fox32
  • 13,126
  • 9
  • 50
  • 71
  • Alright, thanks, that sounds viable. Though I would prefer to just somehow override the original console dumping so it's blocking rather than async. – Mikulas Dite Apr 06 '12 at 10:03
  • The problem is that the object is passed to the console by reference, if you later change the object the output in the console change, too. – Fox32 Apr 06 '12 at 11:09
  • Oh, I see... I thought it does take a snapshot itself. In that case cloning the object seems to be the only solution. Thanks again. – Mikulas Dite Apr 06 '12 at 13:11
  • 1
    I played a little bit with the console, it seem to be passed by reference and if you "open" the object the values are evaluated and stay the same. – Fox32 Apr 06 '12 at 14:07
  • Seems you are right. What that console does is one sick behavior I really don't like. I can't think of any situation this would be either helpful or expected. It just seems so silly. Oh well. Thanks, I guess I will have to go with custom wrappers and cloning. – Mikulas Dite Apr 06 '12 at 19:54
9

I just got caught by this behaviour, spent some hours until I realized the console is borked, not my code. damn.

Until now I only managed to get expected behaviour with:

console.log(JSON.stringify(obj))

nice side effect is, it expands the objects like {0: "a", 3: "b"}

kulpae
  • 3,244
  • 2
  • 25
  • 23
2

Put a breakpoint(see image below) at console.log statement and use controls to step over to next.

enter image description here

Sandeep Manne
  • 6,030
  • 5
  • 39
  • 55