Presenting the code immediately:
setTimeout(() => console.log("next macro")); /// next macro
Promise.resolve().then(() => gen.next()) /// microtask inside, #2
const gen = (function*(){
console.log("Hello");
yield; /// #3
console.log("World");
})();
gen.next();
console.log("main script has been ended"); /// #1
Generators have the ability to be paused. Later on, we can return to the generator function when specific code is called that corresponds with the generator.
This particular code contains 2 macrotasks and 1 microtask. The event loop does not execute microtasks until the current macrotask has finished. Once the main task (#1) is complete, the microtask (#2) will be executed, enabling the code within to bring us back into the generator function (#3), resulting in the execution of console.log("World").
So my question is: what triggers the execution of the generator code, resuming and restoring the context of the generator's execution? Is it a macro or micro task within the event loop when invoked inside a micro task? When executing, is the restored execution context of the generator considered a microtask?
P.S I may not have articulated myself clearly, but I hope you grasp my intention.