Will event be triggered before I set up the listener?

var a = new Image();
...
// Is it possible that `load` happens here, so I won't be able to catch it?
...
a.onload = function () {};

Well, I guess the load event won't be triggered earlier.

How does the browser magically ensures that, by running code round by round? So events only triggered after it finished current round?

The code below won't output anything, is it because by using setTimeout we make foo runs on another round?

var a = require('child_process').spawn('ls', ['-l']);

setTimeout(function foo() {
  a.stdout.on('data', function (data) {
    console.log(data.toString());
  });
}, 1000);

The code will not produce anything, because as you suspected, it completes BEFORE you set up the listener.

Time   Event
00.000 Spawn Child process 12345 `ls` with arguments `-l`  (a)
00.001 Set Timeout for function foo() in 1 second.
00.011 child process 12345 terminated with data. (a)
01.001 Running foo()
01.002 Attach 'data' event to `a`

Basically, you missed the bus (literally if you think of it as a serial bus)

You SHOULD not use the setTimeout and chain it if you want it to catch it.

var a = require('child_process').spawn('ls', ['-l']).stdout.on('data', function (data) {
  console.log(data.toString());
});

How does the browser magically ensures that, by running code round by round? So events only triggered after it finished current round?

Yes. Or at least it should, I don't think it is 100% ensured in all browsers and all edge cases (for example cached files). So you should start the loading (by assigning the src property) always after setting up the listeners:

var a = new Image();
// Is it possible that `load` happens here? No.
a.onload = function () {};
a.src = "";
// Is it possible that `load` happens here? Yes

The code below won't output anything, is it because by using setTimeout we make foo runs on another round?

Exactly. Of course, it could somehow (unlikely) happen that your server is slow and ls -l takes more than a second to before output, then you'd catch it.