ECMAScript defines 2 types of work as required: Promise Jobs and Script Jobs. The situation with Promise Jobs is clear: I created a Promise or an asynchronous function for myself - and everything, the new Promise Job has been created and added to the queue.

But what about Script Jobs? What needs to be done to create a job of this type?

I know that the module code is also related to Script Jobs. But doesn’t the module import its code immediately? Although the specification says that the work is needed in order to be “postponed” and executed later (as in the situation with Promise).

In short, what needs to be done to initiate a new job and add it to the Script Jobs queue?

  • add a link to the specification where you got the definitions and descriptions from - Grundy
  • @Grundy in the first paragraph resulted. - smellyshovel

1 answer 1

The only mention of ScriptJob in terms of enqueuement refers to item 8.6 RunJobs - in the second step it says:

  1. In the implementation of the ECMAScript scripts and / or ECMAScript modules. For each such sourceText and hostDefined, do [...]

It says something like this: you need to select those scripts that are destined to be executed - and put them in a queue. Next, in the third step, their execution is described in order.

In turn, RunJobs in other places of the specification is not mentioned, it is an entry point.


It turns out that ScriptJob is any code that the host has decided to download and execute for whatever reason. The key point of this description is that ScriptJob queuing and execution of all queues are two stages of the same RunJobs operation, thus, according to the specification, it is impossible to execute arbitrary code and not execute pending continuations (PromiseJob).

Why everything is so difficult to paint - I do not know. Apparently, they are trying to make such a specification so that it could not, in principle, be read in different ways (or simply could not be read)

  • It turns out that the execution of the main script, for example, on a page in the browser implies that this script is first placed in the queue, and then immediately removed from this queue and executed? - smellyshovel pm
  • Yes. But just on the page in the browser, a whole bunch of scripts can be connected, and some of them are deferred or asynchronous (with attributes defer or async). - Pavel Mayorov pm
  • But it still does not explain the problem of the modules: if the module code is also placed in the queue during the execution of the main script, how it is executed (and we do have access to the functions obtained from the modules already during the execution of the main script), if the specification in the same part that I quoted in the question clearly says that 2 jobs from one queue cannot be performed simultaneously? - smellyshovel
  • That is, if the module code is executed only after the main script has been executed, then where does this main script have access to what is imported by the module? - smellyshovel
  • @smellyshovel any module starts to run strictly after its dependencies. While at least one import is not loaded or not completed, not a single line of your code will be executed. - Pavel Mayorov