Saturday 22 April 2017

why is the nodejs event loop non-deterministic after the first run?

Node.js doc says:

The order in which the timers are executed will vary depending on the context in which they are called. If both are called from within the main module, then timing will be bound by the performance of the process (which can be impacted by other applications running on the machine).

For example, if we run the following script which is not within an I/O cycle (i.e. the main module), the order in which the two timers are executed is non-deterministic, as it is bound by the performance of the process:

// timeout_vs_immediate.js
setTimeout(function timeout () {
 console.log('timeout');
},0);

setImmediate(function immediate () {
  console.log('immediate');
});

$ node timeout_vs_immediate.js
timeout
immediate

$ node timeout_vs_immediate.js
immediate
timeout

From the same page I think I understand how the event loop works, but after this main run, why can the even loop not fulfill its job correctly ? What's so different there than within an I/O cycle ?



via fabmlk

No comments:

Post a Comment