2

this Question About ECMAScript Specification(ECMA-262 8th edition)

These Days, I'm little confused about Job and Job Queue.

Here is Some Questions.

1: In ECMA-262, There is two kinds of Job Queue. one is ScriptJobs the other is PromiseJobs. So, Witch one have preference?
2: In ECMA-262, There is only definition of RunJobs abstract operation. I want to know when and where the RunJobs execute?
3.I executed code blow, In FF 60.

<!DOCTYPE html>
  <html lang="en">
    <head>
      <meta charset="UTF-8">
      <title>Title</title>
      <script>
        window.addEventListener('DOMContentLoaded', () => {
          let x = document.createElement('script');
          x.innerHTML = 'console.log(3);';
          console.log('script start');
          document.body.appendChild(x);
          setTimeout(() => console.log(1), 0);
          Promise.resolve(2).then(console.log);
          console.log('script is end');
        });
      </script>
    </head>
   <body>
   </body>
  </html>

and it logs :

script start
3
script end
2
1

Why script element execute on excution context that created dynamically?

c. Let nextQueue be a non‐empty Job Queue chosen in an implementation‐defined manner. If all Job Queues are empty, the result is implementation‐defined.

forked from ECMA-262 RunJobs. How HTML spec define implementation‐defined manner?

@Bergi like this? PromiseJobs : [] , ScriptJobs : []

ScriptJobs.push(something);   
// pop and run by Event loop   
// blow will happens while `something` runs 
PromiseJobs.push(anotherOne);
ScriptJobs.push(theother);     
//end of `something`    
//and in here, PromiseJobs will pop?

1 Answers1

5

Which one have preference?

Neither. The spec states "This specification does not define the order in which multiple Job Queues are serviced." The queues are only a tool to specify that per queue, jobs are executed in a FIFO order.

I want to know when and where the RunJobs execute?

That depends very much on the implementation. In an environment like node.js, it would be called when the process is started - it's the basic event loop formulation. Of course there would be more job queues, e.g. for timers or asynchronous IO operations.

Why script element execute on execution context that created dynamically?

Because the HTML spec is weird. Have a look at https://www.html5rocks.com/en/tutorials/speed/script-loading/ or load and execute order of scripts for an overview. The details can be found in the HTML5 processing model for script elements, which basically goes

  • When the script element becomes connected (i.e. is inserted into the DOM),
  • prepare everything and load it (if necessary) and do all kinds of checks,
  • if the element does not have a src attribute and is a classic script (not a module) and is not parser-inserted,
  • then "Immediately execute the script block, even if other scripts are already executing."

Yes, this totally invalidated the ECMAScript queue model for browsers. Which is also explicitly state in the section Integration with the JavaScript job queue.

Bergi
  • 513,640
  • 108
  • 821
  • 1,164