JavaScript Runtime Environment
Components of a JavaScript Runtime Environment
- Call Stack
- Web APIs
- Macro Task Queue (Task Queue)
- Micro Task Queue (Job Queue)
- Event Loop
Key Note
- The Micro Task Queue (Job Queue) has a higher priority than the Macro Task Queue (Task Queue).
Common Examples of Micro Tasks
queueMicrotask(() => console.log("Micro Task 1")); // We can create a micro task using queueMicrotask() function.
Promise.resolve().then(() => console.log("Micro Task 2")); // We can create a micro task using Promise.
async function asyncFunction() {
await console.log("Micro Task 3");
}
asyncFunction();
Question 1
Execution Order
Explanation:
- The body of the async function or a new promise constructor is executed synchronously.
- The Micro Task Queue is executed.
- The Event Loop checks the Micro Task Queue and executes the micro tasks.
- The Macro Task Queue is executed.
- The Event Loop checks the Macro Task Queue and executes the macro tasks.
Answer: 4, 5, 6, 1, 3
Promise.resolve().then(() => {
console.log("1");
});
setTimeout(() => console.log(3), 0);
console.log(4);
new Promise(() => console.log(5));
(async () => console.log(6))();
Question 2
Answer: 4, 5, 1, 2, 8, 7, 6, 3
async function someFunction() {
console.log("1");
new Promise(() => console.log("2"));
await new Promise((res) => {
setTimeout(() => console.log(3), 0);
res(1);
});
}
new Promise((res) => {
console.log("4");
(async () => {
console.log("5");
await someFunction();
console.log("6");
})();
res(2);
}).then(() => console.log(7));
console.log(8);