0

I read that the event loop isn't part of the v8 engine and the host environment should implement it but on the other hand, the promises are part of the v8 engine and for the promises to work, it needs a microtask queue. So my question is: does v8 have a micro task queue internally? Or does the host environment provide it to v8? I'm really confused.

Some references please if you could help

  • This [v8:MicroTaskQueue](https://v8docs.nodesource.com/node-12.0/db/d08/classv8_1_1_microtask_queue.html) reference looks like it's part of V8. Keep in mind that since microTasks are executed before event loop things, the V8 engine can check microTask queues BEFORE returning control to the event loop. – jfriend00 Nov 12 '22 at 22:51
  • Can you provide a link to any reference about promises and V8, specifically? I'm surprised to hear that promises have any sort of impact on the engine at all. It seems to be just a useful addon. – Brad Nov 12 '22 at 22:57
  • 1
    @Brad - Promises are a built-in part of the ECMAScript specification nowadays so for V8 to implement the ECMAScript specification, it has to implement promises too. They used to be a separate "addon" as you say until ECMAScript 2015 when they were added to the official language specification. You can look in any recent ECMAScript specification and find promises as part of it. And, then `async` and `await` were added soon after as built-in language syntax and they also require promises. – jfriend00 Nov 12 '22 at 23:09
  • 2
    Microtasks are not really asynchronous from the engine pov. They are all executed in a tight loop, there is no waiting for new events to come in from the outside. That - the macrotask event loop - still needs to be provided by the embedder. – Bergi Nov 12 '22 at 23:13
  • *I read that* ,,, where did you read this? – Jaromanda X Nov 12 '22 at 23:14
  • Well, here's a nice gray answer. In the ECMAScript spec is [HostEnqueuePromiseJob](https://tc39.es/ecma262/#sec-hostenqueuepromisejob) which is an interface for promise jobs that the host must implement. So, it appears that the ECMAScript spec defines the interface, but the host environment must implement it and can thus integrate it in with the event loop. – jfriend00 Nov 12 '22 at 23:14
  • @jfriend00 That's a spec answer, not an implementation answer. There's no reason for V8 to implement exactly that interface that ECMAScript uses as a touchpoint with other specifications. – Bergi Nov 12 '22 at 23:18
  • @Bergi - What about the V8:MicroTaskQueue link in my first comment. Does that not imply that microTasks are built into the V8 implementation? – jfriend00 Nov 13 '22 at 00:07
  • @jfriend00 yes, V8 has a microtask queue, and promises don't need the event loop, and V8 also has an event loop (see dupe). – jmrk Nov 13 '22 at 00:11

0 Answers0