javascript - What function gets put into EventLoop in NodeJs and JS -


i've been reading nodejs articles in order understand async nature during found , liked node.js, doctor’s offices , fast food restaurants – understanding event-driven programming

there thing called eventloop fifo based queue. when async function gets hit, gets put eventloop , continue executed on there.

i little confused here. example said here:

in actuality, async functions settimeout , setinterval pushed onto queue known event loop.

and in same article:

the event loop queue of callback functions. when async function executes, callback function pushed queue. javascript engine doesn't start processing event loop until code after async function has executed.

but different image:

enter image description here

let's @ following example:

console.log("hello,"); settimeout(function(){console.log("world");},0); 

so understand different explanations,

  1. first 1 says function(){console.log("world");} part of settimeout() function, callback, put in eventloop. once settimeout done, execute eventloop well.
  2. the other 1 says, whole thing settimeout(function(){console.log("world");},0); put eventloop , executed...

i confused more now. should simple guess simple explanation nice me following questions:

  1. which 1 of aforementioned things true?
  2. what eventloop? analogy, real thing methods, objects, etc?
  3. if implement similar thing eventloop scratch, how simply? perhaps code nice see.

i'm going attempt answer, based on mdn's little information regarding event loops. note, answer strictly javascript, not node.

from sound of it, confusion second quote should have been written more clearly, such as:

the event loop queue of callback functions. when async function executes, callback function pushed queue. javascript engine doesn't start continue processing event loop until code after async function has executed.

if that's case, it's more consistent. callbacks, when fire, entered fifo queue, , run sequentially. i.e., if 1 event firing, next 1 won't fire until first 1 completes.

the mdn reference linked above includes pseudo-code event loop:

while(queue.waitformessage()){     queue.processnextmessage(); } 

timers , intervals defined part of dom in html spec. couldn't find direct reference "message loop" in quick check of spec.


Comments

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -