Microtasks and Macrotasks
Understand microtasks and macrotasks
AdvancedTopic: Advanced JS
JavaScript Microtasks and Macrotasks Program
This program helps you to learn the fundamental structure and syntax of JavaScript programming.
console.log('1');
setTimeout(() => console.log('2'), 0);
queueMicrotask(() => console.log('3'));
Promise.resolve().then(() => console.log('4'));
console.log('5');Output
1 5 3 4 2
Understanding Microtasks and Macrotasks
Microtasks execute before macrotasks.
Note: To write and run JavaScript programs, you need to set up the local environment on your computer. Refer to the complete article Setting up JavaScript Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your JavaScript programs.