SlideShare a Scribd company logo
Callback execution strategy in browser
using task and micro task queues
- Jitendra Kasaudhan
- Web developer @check24de
- Twitter: @jitubutwal144
Quiz 1 - Codepen example
console.log("Start");
setTimeout(function CB1() {
console.log("Settimeout 1");
}, 0);
setTimeout(function CB2() {
console.log("Settimeout 2");
}, 0);
Promise.resolve().then(function CB3() {
console.log("Promise 1");
});
Promise.resolve().then(function CB4() {
console.log("Promise 2");
});
console.log("End");
Option 1
Start
End
Settimeout 1
Settimeout 2
Promise 1
Promise 2
Option 2
Start
End
Promise 1
Promise 2
Settimeout 1
Settimeout 2
Option 3
None of above
Quiz 1 - Codepen example
console.log("Start");
setTimeout(function CB1() {
console.log("Settimeout 1");
}, 0);
setTimeout(function CB2() {
console.log("Settimeout 2");
}, 0);
Promise.resolve().then(function CB3() {
console.log("Promise 1");
});
Promise.resolve().then(function CB4() {
console.log("Promise 2");
});
console.log("End");
Option 2
Start
End
Promise 1
Promise 2
Settimeout 1
Settimeout 2
Option 1
Start
End
Settimeout 1
Settimeout 2
Promise 1
Promise 2
Option 3
None of above
What the heck is event loop anyway?
Source: https://www.youtube.com/watch?v=8aGhZQkoFbQ - By @philip_roberts
Javascript Runtime
Javascript Runtime with micro task queue
Sources of callback function
Group 1
- Keyboard events (keydown, keyup
etc)
- Mouse events (click, mouseup,
mousedown etc)
- Network events (online, offline)
- Drag and drop events (dragstart,
dragend )etc
-Timer events (setTimeout(…),
setInterval(…))
Group 2
- Promises (resolve(), reject())
- Browser observers
- Mutation observer
- Intersection Observer
- Performance Observer
- Resize Observer
Callbacks and queues used
Task queue (Group 1)
- Keyboard events (keydown, keyup
etc)
- Mouse events (click, mouseup,
mousedown etc)
- Network events (online, offline)
- Drag and drop events (dragstart,
dragend )etc
-Timer events (setTimeout(…),
setInterval(…))
Micro task queue (Group 2)
- Promises (resolve(), reject())
- Browser observers
- Mutation observer
- Intersection Observer
- Performance Observer
- Resize Observer
Event loop
- The event loop is the mastermind that orchestrates:
- What JavaScript code gets executed?
- When does it run?
- When do layout and style get updated?
- When do DOM changes get rendered?
- Responsible to pick up the task from Task queue or Micro task queue
and execute it in the callstack.
Event loop - pseudo code
while(true) {
task = eventLoop.nextTask();
// execute callback in the task queue
if (task) {
task.execute();
}
// execute all callbacks in the micro task queue
eventLoop.executeMicrotasks();
if (eventLoop.needsRendering()) {
eventLoop.render();
}
}
Execution strategy
- If one task contains multiple micro tasks, all the callbacks queued in
Micro task queue is executed first before picking up the next task
from the task queue.
- Before executing next task from the task queue, callstack should be
empty.
Quiz 1 - answer
Quiz 2-two click listeners for one button
Option 1
Listener 1
Promise 1
Listener 2
Promise 2
Settimeout 1
Settimeout 2
Option 2
Listener 1
Settimeout 1
Promise 1
Listener 2
Settimeout 2
Promise 2
var button = document.querySelector(".button");
button.addEventListener("click", function CB1() {
console.log("Listener 1");
setTimeout(function ST1() {
console.log("Settimeout 1");
}, 0);
Promise.resolve().then(function P1() {
console.log("Promise 1");
});
});
button.addEventListener("click", function CB2() {
console.log("Listener 2");
setTimeout(function ST2() {
console.log("Settimeout 2");
}, 0);
Promise.resolve().then(function P2() {
console.log("Promise 2");
});
});
Option 3
None of above
Quiz 2-two click listeners for one button
Option 1
Listener 1
Promise 1
Listener 2
Promise 2
Settimeout 1
Settimeout 2
Option 2
Listener 1
Settimeout 1
Promise 1
Listener 2
Settimeout 2
Promise 2
var button = document.querySelector(".button");
button.addEventListener("click", function CB1() {
console.log("Listener 1");
setTimeout(function ST1() {
console.log("Settimeout 1");
}, 0);
Promise.resolve().then(function P1() {
console.log("Promise 1");
});
});
button.addEventListener("click", function CB2() {
console.log("Listener 2");
setTimeout(function ST2() {
console.log("Settimeout 2");
}, 0);
Promise.resolve().then(function P2() {
console.log("Promise 2");
});
});
Option 3
None of above
Quiz 2 - answer
Quiz 3 - answer
Quiz 3 - answer
Thank You!!
References
- What the heck is event loop anyway ?
- Jake Archibald: In The Loop - JSConf.Asia 2018
- Event loop explainer - and its pseudo code
- JavaScript: How is callback execution strategy for promises
different than DOM events callback?

More Related Content

What's hot (20)

PPTX
Java practice programs for beginners
ishan0019
 
PDF
The Ring programming language version 1.8 book - Part 88 of 202
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.5.1 book - Part 25 of 180
Mahmoud Samir Fayed
 
PPT
Ch7 Process Synchronization galvin
Shubham Singh
 
PPTX
Scheduling in Linux and Web Servers
David Evans
 
PPT
Microkernel Development
Rodrigo Almeida
 
PPT
OS Process Synchronization, semaphore and Monitors
sgpraju
 
PPT
Chapter 6 - Process Synchronization
Wayne Jones Jnr
 
PDF
The Ring programming language version 1.10 book - Part 11 of 212
Mahmoud Samir Fayed
 
PDF
Operating System-Ch6 process synchronization
Syaiful Ahdan
 
PDF
Kernel Recipes 2019 - RCU in 2019 - Joel Fernandes
Anne Nicolas
 
PDF
Ownership System in Rust
Chih-Hsuan Kuo
 
PDF
Advanced patterns in asynchronous programming
Michael Arenzon
 
PDF
The Ring programming language version 1.9 book - Part 33 of 210
Mahmoud Samir Fayed
 
PPTX
Cs1123 6 loops
TAlha MAlik
 
PPTX
Introduction to ParSeq: to make asynchronous java easier
Junchuan Wang
 
PPTX
SSL Failing, Sharing, and Scheduling
David Evans
 
PDF
Protocol handler in Gecko
Chih-Hsuan Kuo
 
PDF
The Ring programming language version 1.10 book - Part 35 of 212
Mahmoud Samir Fayed
 
Java practice programs for beginners
ishan0019
 
The Ring programming language version 1.8 book - Part 88 of 202
Mahmoud Samir Fayed
 
The Ring programming language version 1.5.1 book - Part 25 of 180
Mahmoud Samir Fayed
 
Ch7 Process Synchronization galvin
Shubham Singh
 
Scheduling in Linux and Web Servers
David Evans
 
Microkernel Development
Rodrigo Almeida
 
OS Process Synchronization, semaphore and Monitors
sgpraju
 
Chapter 6 - Process Synchronization
Wayne Jones Jnr
 
The Ring programming language version 1.10 book - Part 11 of 212
Mahmoud Samir Fayed
 
Operating System-Ch6 process synchronization
Syaiful Ahdan
 
Kernel Recipes 2019 - RCU in 2019 - Joel Fernandes
Anne Nicolas
 
Ownership System in Rust
Chih-Hsuan Kuo
 
Advanced patterns in asynchronous programming
Michael Arenzon
 
The Ring programming language version 1.9 book - Part 33 of 210
Mahmoud Samir Fayed
 
Cs1123 6 loops
TAlha MAlik
 
Introduction to ParSeq: to make asynchronous java easier
Junchuan Wang
 
SSL Failing, Sharing, and Scheduling
David Evans
 
Protocol handler in Gecko
Chih-Hsuan Kuo
 
The Ring programming language version 1.10 book - Part 35 of 212
Mahmoud Samir Fayed
 

Similar to Lightining Talk - Task queue and micro task queues in browser (20)

PPTX
JavaScript Multithread or Single Thread.pptx
RAHITNATH
 
PDF
Deep Dive into Zone.JS
Ilia Idakiev
 
PDF
JavaScript Async for Effortless UX
재석 강
 
PPTX
Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...
Domenic Denicola
 
PDF
Understanding Asynchronous JavaScript
jnewmanux
 
PPTX
Events for JavaScript event loop track.pptx
sontinenianuradha
 
PPTX
All you need to know about the JavaScript event loop
Saša Tatar
 
PPTX
Call stack, event loop and async programming
Masters Academy
 
PDF
JavaScript Promise
Joseph Chiang
 
PDF
Douglas Crockford: Serversideness
WebExpo
 
PDF
The evolution of java script asynchronous calls
Huy Hoàng Phạm
 
PDF
Sane Async Patterns
TrevorBurnham
 
PDF
Intro to Asynchronous Javascript
Garrett Welson
 
PDF
Event Driven Javascript
Federico Galassi
 
PDF
Asynchronous programming done right - Node.js
Piotr Pelczar
 
PDF
Promise: async programming hero
The Software House
 
PPT
Web development basics (Part-5)
Rajat Pratap Singh
 
PPTX
Node js for backend server development.
digitalindia1231
 
PDF
Event driven javascript
Francesca1980
 
PDF
Event driven javascript
Francesca1980
 
JavaScript Multithread or Single Thread.pptx
RAHITNATH
 
Deep Dive into Zone.JS
Ilia Idakiev
 
JavaScript Async for Effortless UX
재석 강
 
Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...
Domenic Denicola
 
Understanding Asynchronous JavaScript
jnewmanux
 
Events for JavaScript event loop track.pptx
sontinenianuradha
 
All you need to know about the JavaScript event loop
Saša Tatar
 
Call stack, event loop and async programming
Masters Academy
 
JavaScript Promise
Joseph Chiang
 
Douglas Crockford: Serversideness
WebExpo
 
The evolution of java script asynchronous calls
Huy Hoàng Phạm
 
Sane Async Patterns
TrevorBurnham
 
Intro to Asynchronous Javascript
Garrett Welson
 
Event Driven Javascript
Federico Galassi
 
Asynchronous programming done right - Node.js
Piotr Pelczar
 
Promise: async programming hero
The Software House
 
Web development basics (Part-5)
Rajat Pratap Singh
 
Node js for backend server development.
digitalindia1231
 
Event driven javascript
Francesca1980
 
Event driven javascript
Francesca1980
 
Ad

More from Jitendra Kasaudhan (6)

PDF
Seminar paperonconversationalandcritiquesbasedrecomendersystems jitendratum
Jitendra Kasaudhan
 
PDF
Jitendra broschuere church_oflight_git
Jitendra Kasaudhan
 
PDF
Business Model Canvas For Teaching Mediation Platform (TeachZone)
Jitendra Kasaudhan
 
PPTX
Business Model Canvas For Teaching Mediation Platform
Jitendra Kasaudhan
 
PDF
Diverse culture presentation of architectures
Jitendra Kasaudhan
 
PPTX
Mongodb
Jitendra Kasaudhan
 
Seminar paperonconversationalandcritiquesbasedrecomendersystems jitendratum
Jitendra Kasaudhan
 
Jitendra broschuere church_oflight_git
Jitendra Kasaudhan
 
Business Model Canvas For Teaching Mediation Platform (TeachZone)
Jitendra Kasaudhan
 
Business Model Canvas For Teaching Mediation Platform
Jitendra Kasaudhan
 
Diverse culture presentation of architectures
Jitendra Kasaudhan
 
Ad

Recently uploaded (20)

PDF
Presentation - Vibe Coding The Future of Tech
yanuarsinggih1
 
PDF
SWEBOK Guide and Software Services Engineering Education
Hironori Washizaki
 
PPTX
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
PDF
Complete JavaScript Notes: From Basics to Advanced Concepts.pdf
haydendavispro
 
PDF
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
PDF
From Code to Challenge: Crafting Skill-Based Games That Engage and Reward
aiyshauae
 
PDF
NewMind AI - Journal 100 Insights After The 100th Issue
NewMind AI
 
PDF
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
PPTX
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
PDF
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
PDF
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PDF
LLMs.txt: Easily Control How AI Crawls Your Site
Keploy
 
PDF
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
PPTX
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
PPTX
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
PDF
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
PDF
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
PDF
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
PDF
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
PDF
HubSpot Main Hub: A Unified Growth Platform
Jaswinder Singh
 
Presentation - Vibe Coding The Future of Tech
yanuarsinggih1
 
SWEBOK Guide and Software Services Engineering Education
Hironori Washizaki
 
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
Complete JavaScript Notes: From Basics to Advanced Concepts.pdf
haydendavispro
 
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
From Code to Challenge: Crafting Skill-Based Games That Engage and Reward
aiyshauae
 
NewMind AI - Journal 100 Insights After The 100th Issue
NewMind AI
 
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
LLMs.txt: Easily Control How AI Crawls Your Site
Keploy
 
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
HubSpot Main Hub: A Unified Growth Platform
Jaswinder Singh
 

Lightining Talk - Task queue and micro task queues in browser

  • 1. Callback execution strategy in browser using task and micro task queues - Jitendra Kasaudhan - Web developer @check24de - Twitter: @jitubutwal144
  • 2. Quiz 1 - Codepen example console.log("Start"); setTimeout(function CB1() { console.log("Settimeout 1"); }, 0); setTimeout(function CB2() { console.log("Settimeout 2"); }, 0); Promise.resolve().then(function CB3() { console.log("Promise 1"); }); Promise.resolve().then(function CB4() { console.log("Promise 2"); }); console.log("End"); Option 1 Start End Settimeout 1 Settimeout 2 Promise 1 Promise 2 Option 2 Start End Promise 1 Promise 2 Settimeout 1 Settimeout 2 Option 3 None of above
  • 3. Quiz 1 - Codepen example console.log("Start"); setTimeout(function CB1() { console.log("Settimeout 1"); }, 0); setTimeout(function CB2() { console.log("Settimeout 2"); }, 0); Promise.resolve().then(function CB3() { console.log("Promise 1"); }); Promise.resolve().then(function CB4() { console.log("Promise 2"); }); console.log("End"); Option 2 Start End Promise 1 Promise 2 Settimeout 1 Settimeout 2 Option 1 Start End Settimeout 1 Settimeout 2 Promise 1 Promise 2 Option 3 None of above
  • 4. What the heck is event loop anyway? Source: https://www.youtube.com/watch?v=8aGhZQkoFbQ - By @philip_roberts
  • 6. Javascript Runtime with micro task queue
  • 7. Sources of callback function Group 1 - Keyboard events (keydown, keyup etc) - Mouse events (click, mouseup, mousedown etc) - Network events (online, offline) - Drag and drop events (dragstart, dragend )etc -Timer events (setTimeout(…), setInterval(…)) Group 2 - Promises (resolve(), reject()) - Browser observers - Mutation observer - Intersection Observer - Performance Observer - Resize Observer
  • 8. Callbacks and queues used Task queue (Group 1) - Keyboard events (keydown, keyup etc) - Mouse events (click, mouseup, mousedown etc) - Network events (online, offline) - Drag and drop events (dragstart, dragend )etc -Timer events (setTimeout(…), setInterval(…)) Micro task queue (Group 2) - Promises (resolve(), reject()) - Browser observers - Mutation observer - Intersection Observer - Performance Observer - Resize Observer
  • 9. Event loop - The event loop is the mastermind that orchestrates: - What JavaScript code gets executed? - When does it run? - When do layout and style get updated? - When do DOM changes get rendered? - Responsible to pick up the task from Task queue or Micro task queue and execute it in the callstack.
  • 10. Event loop - pseudo code while(true) { task = eventLoop.nextTask(); // execute callback in the task queue if (task) { task.execute(); } // execute all callbacks in the micro task queue eventLoop.executeMicrotasks(); if (eventLoop.needsRendering()) { eventLoop.render(); } }
  • 11. Execution strategy - If one task contains multiple micro tasks, all the callbacks queued in Micro task queue is executed first before picking up the next task from the task queue. - Before executing next task from the task queue, callstack should be empty.
  • 12. Quiz 1 - answer
  • 13. Quiz 2-two click listeners for one button Option 1 Listener 1 Promise 1 Listener 2 Promise 2 Settimeout 1 Settimeout 2 Option 2 Listener 1 Settimeout 1 Promise 1 Listener 2 Settimeout 2 Promise 2 var button = document.querySelector(".button"); button.addEventListener("click", function CB1() { console.log("Listener 1"); setTimeout(function ST1() { console.log("Settimeout 1"); }, 0); Promise.resolve().then(function P1() { console.log("Promise 1"); }); }); button.addEventListener("click", function CB2() { console.log("Listener 2"); setTimeout(function ST2() { console.log("Settimeout 2"); }, 0); Promise.resolve().then(function P2() { console.log("Promise 2"); }); }); Option 3 None of above
  • 14. Quiz 2-two click listeners for one button Option 1 Listener 1 Promise 1 Listener 2 Promise 2 Settimeout 1 Settimeout 2 Option 2 Listener 1 Settimeout 1 Promise 1 Listener 2 Settimeout 2 Promise 2 var button = document.querySelector(".button"); button.addEventListener("click", function CB1() { console.log("Listener 1"); setTimeout(function ST1() { console.log("Settimeout 1"); }, 0); Promise.resolve().then(function P1() { console.log("Promise 1"); }); }); button.addEventListener("click", function CB2() { console.log("Listener 2"); setTimeout(function ST2() { console.log("Settimeout 2"); }, 0); Promise.resolve().then(function P2() { console.log("Promise 2"); }); }); Option 3 None of above
  • 15. Quiz 2 - answer
  • 16. Quiz 3 - answer
  • 17. Quiz 3 - answer
  • 19. References - What the heck is event loop anyway ? - Jake Archibald: In The Loop - JSConf.Asia 2018 - Event loop explainer - and its pseudo code - JavaScript: How is callback execution strategy for promises different than DOM events callback?