queuemicrotask callback code example
Example: queuemicrotask
setTimeout(() => {
console.log('hey i am executed asychronously by setTimeout');
},0);
queueMicrotask(() => {
console.log('hey i am executed asychronously by queueMicrotask');
});
output:-
hey i am executed asychronously by queueMicrotask
hey i am executed asychronously by setTimeout
//reason:- queueMicrotasks are executed after the call stack is empty and before the control is
send back to the event loop puts the microtask/macrotask of event queue back to the call stack.