SlideShare a Scribd company logo
JavaScript Multi Thread or
Single Thread?
-By Rahit Nath
What is
JavaScript?
→ JavaScript is a programming language that is
commonly used to add interactivity and
functionality to web pages. It is an object-
oriented language that is primarily used in
front-end web development, allowing
developers to create dynamic and interactive
user interfaces.
How JavaScript
Works?
(Example-1)
function a(){
console.log(a);
}
a();
console.log("Hello World!");
w
Memory Heap
Call Stack
How JavaScript
Works?
function a(){
console.log(a);
}
a();
console.log("Hello World!");
w
Memory Heap
Call Stack
Memory Heap
function a(){
console.log(‘a’);
}
a();
console.log("Hello World!");
w
a()
Display Window
Call Stack
Memory Heap
function a(){
console.log(‘a’);
}
a();
console.log("Hello World!");
w
a()
a
Display Window
Call Stack
Memory Heap
console.log("
a");
function a(){
console.log(‘a’);
}
a();
console.log("Hello World!");
w
a()
a
Display Window
Call Stack
Memory Heap
function a(){
console.log(‘a’);
}
a();
console.log("Hello World!");
w a
Display Window
Call Stack
Memory Heap
function a(){
console.log(‘a’);
}
a();
console.log("Hello World!");
w
console.log("
Hello
World!");
a
Hello World!
Memory Heap Call Stack
Display Window
function a(){
console.log(‘a’);
}
a();
console.log("Hello World!");
w a
Hello World!
Memory Heap Call Stack
Display Window
Is JavaScript
Single-Thread?
→ Yes!, JavaScript is Single Threaded
language.
→ It have a single Call Stack Memory where
the programs are executed line by line
→ In hierarchical programming where on
function calls the other function, the
functions are stacked and executed one by
one.
How JavaScript
Works?
(Example-2)
function b(){
console.log(“Inside B”);
}
function a(){
console.log(a);
b();
}
a();
console.log("Hello World!");
w
Call Stack
function b(){
console.log(“Inside B”);
}
function a(){
console.log(a);
b();
}
a();
console.log("Hello World!");
w
Call Stack
a();
Display Window
function b(){
console.log(“Inside B”);
}
function a(){
console.log(a);
b();
}
a();
console.log("Hello World!");
w
Call Stack
a();
a
Display Window
console.log("
a");
function b(){
console.log(“Inside B”);
}
function a(){
console.log(a);
b();
}
a();
console.log("Hello World!");
w
Call Stack
a();
b();
a
Display Window
function b(){
console.log(“Inside B”);
}
function a(){
console.log(a);
b();
}
a();
console.log("Hello World!");
w
Call Stack
a();
b(); a
Inside B
Display Window
console.log("I
nside B");
function b(){
console.log(“Inside B”);
}
function a(){
console.log(a);
b();
}
a();
console.log("Hello World!");
w
Call Stack
a();
b();
Display Window
a
Inside B
function b(){
console.log(“Inside B”);
}
function a(){
console.log(a);
b();
}
a();
console.log("Hello World!");
w
Call Stack
a();
Display Window
a
Inside B
function b(){
console.log(“Inside B”);
}
function a(){
console.log(a);
b();
}
a();
console.log("Hello World!");
w
Call Stack Display Window
a
Inside B
Hello World!
console.log("
Hello
World!");
function b(){
console.log(“Inside B”);
}
function a(){
console.log(a);
b();
}
a();
console.log("Hello World!");
w
Call Stack Display Window
a
Inside B
Hello World!
What is the
Issue then?
By definition - Single thread operation uses
one stack memory to execute the programs
line by line
If there is a delay in between any of the
execution, the whole stack have to wait for
the execution to get complete and then the
next block will get executed.
Issue In multi
threading
(Example-3)
function b(){
console.log(“Inside B”);
for(var i=0;i<10000;i++){
// execute some 10000 line of statements
}
}
function a(){
console.log(a);
b();
}
a();
console.log("Hello World!");
w
Call Stack
w
Call Stack
a();
Display Window
function b(){
console.log(“Inside B”);
for(var i=0;i<10000;i++){
// execute some 10000 line of statements
}
}
function a(){
console.log(a);
b();
}
a();
console.log("Hello World!");
w
Call Stack
a();
a
Display Window
function b(){
console.log(“Inside B”);
for(var i=0;i<10000;i++){
// execute some 10000 line of statements
}
}
function a(){
console.log(a);
b();
}
a();
console.log("Hello World!");
console.log(a)
;
w
Call Stack
a();
a
Display Window
function b(){
console.log(“Inside B”);
for(var i=0;i<10000;i++){
// execute some 10000 line of statements
}
}
function a(){
console.log(a);
b();
}
a();
console.log("Hello World!");
b()
w
Call Stack
a();
a
Inside B
Display Window
function b(){
console.log(“Inside B”);
for(var i=0;i<10000;i++){
// execute some 10000 line of statements
}
}
function a(){
console.log(a);
b();
}
a();
console.log("Hello World!");
b()
console.log(“I
nside B”);
w
Call Stack
a();
a
Inside B
Display Window
function b(){
console.log(“Inside B”);
for(var i=0;i<10000;i++){
// execute some 10000 line of statements
}
}
function a(){
console.log(a);
b();
}
a();
console.log("Hello World!");
b()
Some Big
Execution
What to do
next?
→ Here comes the non blocking concept of
JavaScript programming
→ In the above example, if we want to
execute the statement after all the other
statement gets executed, we can achieve
with the non blocking techniques
Lets know
about few
jargons.
→ WEB API -
→ Callback Queue -
→ Event Loop -
What is WEB
API?
Web APIs play a significant role in implementing non-blocking
techniques in JavaScript, which allows for more efficient and responsive
web applications.
Here are some examples of non-blocking techniques in JavaScript that
use Web APIs:
1. Asynchronous JavaScript and XMLHttpRequest (XHR)
2. Web Workers
3. setTimeout and setInterval
4. Promises and async/await
What is
CallBack
Queue? In JavaScript, the Callback Queue is a data structure that stores callback
functions that are ready to be executed. It is part of the event loop, which
is responsible for handling asynchronous operations in JavaScript.
The Callback Queue ensures that asynchronous functions are executed
in the correct order and that they do not block the main thread of the
application
What is Event
Loop? → When an asynchronous operation is completed.
→ The Web API sends the result and any associated callback
functions to the event loop.
→ The event loop then checks if the call stack is empty.
→ If it is, the callback is pushed onto the call stack and executed.
-
→ If the call stack is not empty, the callback is added to the callback
queue to be executed later.
Non Blocking
Techniques of
Js.
(Example- 4)
function getData(url, callback) {
const xhr = new XMLHttpRequest();
xhr.open('GET', url);
xhr.onload = function() {
if (xhr.status === 200) {
callback(null, xhr.response);
} else {
callback(xhr.status);
}
};
xhr.onerror = function() {
callback('Error');
};
xhr.send();
}
getData('https://example.com/api/data', function(err, data) {
if (err) {
console.log('Error: ' + err);
} else {
console.log('Data: ' + data);
}
});
console.log(“Hello World!”);
setTimeOut(()=>{
console.log(“after some timeout”);
},5000)
w
Call Stack
getData();
Display Window
function getData(url, callback) {
const xhr = new XMLHttpRequest();
xhr.open('GET', url);
xhr.onload = function() {
if (xhr.status === 200) {
callback(null, xhr.response);
} else {
callback(xhr.status);
}
};
xhr.onerror = function() {
callback('Error');
};
xhr.send();
}
getData('https://example.com/api/data',
function(err, data) {
if (err) {
console.log('Error: ' + err);
} else {
console.log('Data: ' + data);
}
});
console.log(“Hello World!”);
setTimeOut(()=>{
console.log(“after some timeout”);
},5000)
p2
p1
Callback Queue
Microtask Queue
WEB API
w
Call Stack
getData();
Display Window
function getData(url, callback) {
const xhr = new XMLHttpRequest();
xhr.open('GET', url);
xhr.onload = function() {
if (xhr.status === 200) {
callback(null, xhr.response);
} else {
callback(xhr.status);
}
};
xhr.onerror = function() {
callback('Error');
};
xhr.send();
}
getData('https://example.com/api/data',
function(err, data) {
if (err) {
console.log('Error: ' + err);
} else {
console.log('Data: ' + data);
}
});
console.log(“Hello World!”);
setTimeOut(()=>{
console.log(“after some timeout”);
},5000)
p2
p1
Callback Queue
Microtask Queue
WEB API
xhr.open('GET',
url);
w
Call Stack
Display Window
function getData(url, callback) {
const xhr = new XMLHttpRequest();
xhr.open('GET', url);
xhr.onload = function() {
if (xhr.status === 200) {
callback(null, xhr.response);
} else {
callback(xhr.status);
}
};
xhr.onerror = function() {
callback('Error');
};
xhr.send();
}
getData('https://example.com/api/data',
function(err, data) {
if (err) {
console.log('Error: ' + err);
} else {
console.log('Data: ' + data);
}
});
console.log(“Hello World!”);
setTimeOut(()=>{
console.log(“after some timeout”);
},5000)
p2
p1
Callback Queue
Microtask Queue
WEB API
xhr.open('GET', url);
w
Call Stack
Hello World!
Display Window
function getData(url, callback) {
const xhr = new XMLHttpRequest();
xhr.open('GET', url);
xhr.onload = function() {
if (xhr.status === 200) {
callback(null, xhr.response);
} else {
callback(xhr.status);
}
};
xhr.onerror = function() {
callback('Error');
};
xhr.send();
}
getData('https://example.com/api/data',
function(err, data) {
if (err) {
console.log('Error: ' + err);
} else {
console.log('Data: ' + data);
}
});
console.log(“Hello World!”);
setTimeOut(()=>{
console.log(“after some timeout”);
},5000)
p2
p1
Callback Queue
Microtask Queue
WEB API
xhr.open('GET', url);
console.log(“Hel
lo World!”)
w
Call Stack
Hello World!
Display Window
function getData(url, callback) {
const xhr = new XMLHttpRequest();
xhr.open('GET', url);
xhr.onload = function() {
if (xhr.status === 200) {
callback(null, xhr.response);
} else {
callback(xhr.status);
}
};
xhr.onerror = function() {
callback('Error');
};
xhr.send();
}
getData('https://example.com/api/data',
function(err, data) {
if (err) {
console.log('Error: ' + err);
} else {
console.log('Data: ' + data);
}
});
console.log(“Hello World!”);
setTimeOut(()=>{
console.log(“after some timeout”);
},5000)
p2
p1
Callback Queue
Microtask Queue
WEB API
xhr.open('GET', url);
timeOut()
Case -1 :
Promise
Resolve first
than
setTimeOut
w
Call Stack
Hello World!
<Some XHR Data>
Data
Display Window
p2
p1
Callback Queue
Microtask Queue
WEB API
timeOut()
xhr.open('GET',
url);
Case -2 :
Promise
Resolve after
setTimeOut
w
Call Stack Hello World!
after some timeout
Display Window
p2
p1
Callback Queue
Microtask Queue
WEB API
timeOut()
xhr.open('GET', url);
Case -3 :
Promise
Resolve
together with
setTimeOut
w
Call Stack
Hello World!
Display Window
p2
p1
Callback Queue
WEB API
timeOut()
xhr.open('GET',
url);
Microtask Queue
Case -3 :
Promise
Resolve
together with
setTimeOut
w
Call Stack Hello World!
<Some XHR Data>
Data
Display Window
p2
p1
Callback Queue
WEB API
timeOut()
Microtask Queue
Case -3 :
Promise
Resolve
together with
setTimeOut
w
Call Stack
Hello World!
<Some XHR Data>
Data
after some timeout
Display Window
p2
p1
Callback Queue
WEB API
Microtask Queue
Thank You!

More Related Content

PPTX
JS Event Loop
PPTX
All you need to know about the JavaScript event loop
PDF
JavaScript Async for Effortless UX
PPTX
[DevDay2018] How does JavaScript actually work? - By: Vi Nguyen, Senior Softw...
PPTX
JavaScript Event Loop
PDF
Developing Async Sense
PPT
JavaScript Event Loop
PDF
Asynchronous programming done right - Node.js
JS Event Loop
All you need to know about the JavaScript event loop
JavaScript Async for Effortless UX
[DevDay2018] How does JavaScript actually work? - By: Vi Nguyen, Senior Softw...
JavaScript Event Loop
Developing Async Sense
JavaScript Event Loop
Asynchronous programming done right - Node.js

Similar to JavaScript Multithread or Single Thread.pptx (20)

PDF
Javascript internals
PDF
Understanding Asynchronous JavaScript
PPTX
Events for JavaScript event loop track.pptx
PDF
Promise: async programming hero
PDF
Intro to Asynchronous Javascript
PPT
Web development basics (Part-5)
PDF
Profiling JavaScript Performance
PDF
Frontend Track NodeJS
PDF
JavaScript for real men
PDF
The evolution of java script asynchronous calls
PPTX
The JavaScript Event Loop - Concurrency in the Language of the Web
PPTX
Javascript why what and how
PDF
"Leveraging the Event Loop for Blazing-Fast Applications!", Michael Di Prisco
PDF
JavaScript
PDF
Douglas Crockford: Serversideness
PPTX
Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...
PPTX
Node js for backend server development.
PDF
Lightining Talk - Task queue and micro task queues in browser
PDF
An overview of JavaScript and Node.js
PDF
Про асинхронность / Максим Щепелин / Web Developer Wargaming
Javascript internals
Understanding Asynchronous JavaScript
Events for JavaScript event loop track.pptx
Promise: async programming hero
Intro to Asynchronous Javascript
Web development basics (Part-5)
Profiling JavaScript Performance
Frontend Track NodeJS
JavaScript for real men
The evolution of java script asynchronous calls
The JavaScript Event Loop - Concurrency in the Language of the Web
Javascript why what and how
"Leveraging the Event Loop for Blazing-Fast Applications!", Michael Di Prisco
JavaScript
Douglas Crockford: Serversideness
Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...
Node js for backend server development.
Lightining Talk - Task queue and micro task queues in browser
An overview of JavaScript and Node.js
Про асинхронность / Максим Щепелин / Web Developer Wargaming
Ad

Recently uploaded (20)

PPTX
Safety Seminar civil to be ensured for safe working.
PPTX
Current and future trends in Computer Vision.pptx
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PDF
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PDF
Human-AI Collaboration: Balancing Agentic AI and Autonomy in Hybrid Systems
PPTX
bas. eng. economics group 4 presentation 1.pptx
PPTX
additive manufacturing of ss316l using mig welding
PPTX
Fundamentals of safety and accident prevention -final (1).pptx
DOCX
573137875-Attendance-Management-System-original
PPTX
web development for engineering and engineering
PPTX
UNIT 4 Total Quality Management .pptx
PDF
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PPT
Mechanical Engineering MATERIALS Selection
PPTX
OOP with Java - Java Introduction (Basics)
PPTX
Sustainable Sites - Green Building Construction
PDF
Unit I ESSENTIAL OF DIGITAL MARKETING.pdf
PPTX
Geodesy 1.pptx...............................................
Safety Seminar civil to be ensured for safe working.
Current and future trends in Computer Vision.pptx
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
Embodied AI: Ushering in the Next Era of Intelligent Systems
Human-AI Collaboration: Balancing Agentic AI and Autonomy in Hybrid Systems
bas. eng. economics group 4 presentation 1.pptx
additive manufacturing of ss316l using mig welding
Fundamentals of safety and accident prevention -final (1).pptx
573137875-Attendance-Management-System-original
web development for engineering and engineering
UNIT 4 Total Quality Management .pptx
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
Operating System & Kernel Study Guide-1 - converted.pdf
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
Mechanical Engineering MATERIALS Selection
OOP with Java - Java Introduction (Basics)
Sustainable Sites - Green Building Construction
Unit I ESSENTIAL OF DIGITAL MARKETING.pdf
Geodesy 1.pptx...............................................
Ad

JavaScript Multithread or Single Thread.pptx

  • 1. JavaScript Multi Thread or Single Thread? -By Rahit Nath
  • 2. What is JavaScript? → JavaScript is a programming language that is commonly used to add interactivity and functionality to web pages. It is an object- oriented language that is primarily used in front-end web development, allowing developers to create dynamic and interactive user interfaces.
  • 10. function a(){ console.log(‘a’); } a(); console.log("Hello World!"); w a Hello World! Memory Heap Call Stack Display Window
  • 11. Is JavaScript Single-Thread? → Yes!, JavaScript is Single Threaded language. → It have a single Call Stack Memory where the programs are executed line by line → In hierarchical programming where on function calls the other function, the functions are stacked and executed one by one.
  • 12. How JavaScript Works? (Example-2) function b(){ console.log(“Inside B”); } function a(){ console.log(a); b(); } a(); console.log("Hello World!"); w Call Stack
  • 13. function b(){ console.log(“Inside B”); } function a(){ console.log(a); b(); } a(); console.log("Hello World!"); w Call Stack a(); Display Window
  • 14. function b(){ console.log(“Inside B”); } function a(){ console.log(a); b(); } a(); console.log("Hello World!"); w Call Stack a(); a Display Window console.log(" a");
  • 15. function b(){ console.log(“Inside B”); } function a(){ console.log(a); b(); } a(); console.log("Hello World!"); w Call Stack a(); b(); a Display Window
  • 16. function b(){ console.log(“Inside B”); } function a(){ console.log(a); b(); } a(); console.log("Hello World!"); w Call Stack a(); b(); a Inside B Display Window console.log("I nside B");
  • 17. function b(){ console.log(“Inside B”); } function a(){ console.log(a); b(); } a(); console.log("Hello World!"); w Call Stack a(); b(); Display Window a Inside B
  • 18. function b(){ console.log(“Inside B”); } function a(){ console.log(a); b(); } a(); console.log("Hello World!"); w Call Stack a(); Display Window a Inside B
  • 19. function b(){ console.log(“Inside B”); } function a(){ console.log(a); b(); } a(); console.log("Hello World!"); w Call Stack Display Window a Inside B Hello World! console.log(" Hello World!");
  • 20. function b(){ console.log(“Inside B”); } function a(){ console.log(a); b(); } a(); console.log("Hello World!"); w Call Stack Display Window a Inside B Hello World!
  • 21. What is the Issue then? By definition - Single thread operation uses one stack memory to execute the programs line by line If there is a delay in between any of the execution, the whole stack have to wait for the execution to get complete and then the next block will get executed.
  • 22. Issue In multi threading (Example-3) function b(){ console.log(“Inside B”); for(var i=0;i<10000;i++){ // execute some 10000 line of statements } } function a(){ console.log(a); b(); } a(); console.log("Hello World!"); w Call Stack
  • 23. w Call Stack a(); Display Window function b(){ console.log(“Inside B”); for(var i=0;i<10000;i++){ // execute some 10000 line of statements } } function a(){ console.log(a); b(); } a(); console.log("Hello World!");
  • 24. w Call Stack a(); a Display Window function b(){ console.log(“Inside B”); for(var i=0;i<10000;i++){ // execute some 10000 line of statements } } function a(){ console.log(a); b(); } a(); console.log("Hello World!"); console.log(a) ;
  • 25. w Call Stack a(); a Display Window function b(){ console.log(“Inside B”); for(var i=0;i<10000;i++){ // execute some 10000 line of statements } } function a(){ console.log(a); b(); } a(); console.log("Hello World!"); b()
  • 26. w Call Stack a(); a Inside B Display Window function b(){ console.log(“Inside B”); for(var i=0;i<10000;i++){ // execute some 10000 line of statements } } function a(){ console.log(a); b(); } a(); console.log("Hello World!"); b() console.log(“I nside B”);
  • 27. w Call Stack a(); a Inside B Display Window function b(){ console.log(“Inside B”); for(var i=0;i<10000;i++){ // execute some 10000 line of statements } } function a(){ console.log(a); b(); } a(); console.log("Hello World!"); b() Some Big Execution
  • 28. What to do next? → Here comes the non blocking concept of JavaScript programming → In the above example, if we want to execute the statement after all the other statement gets executed, we can achieve with the non blocking techniques
  • 29. Lets know about few jargons. → WEB API - → Callback Queue - → Event Loop -
  • 30. What is WEB API? Web APIs play a significant role in implementing non-blocking techniques in JavaScript, which allows for more efficient and responsive web applications. Here are some examples of non-blocking techniques in JavaScript that use Web APIs: 1. Asynchronous JavaScript and XMLHttpRequest (XHR) 2. Web Workers 3. setTimeout and setInterval 4. Promises and async/await
  • 31. What is CallBack Queue? In JavaScript, the Callback Queue is a data structure that stores callback functions that are ready to be executed. It is part of the event loop, which is responsible for handling asynchronous operations in JavaScript. The Callback Queue ensures that asynchronous functions are executed in the correct order and that they do not block the main thread of the application
  • 32. What is Event Loop? → When an asynchronous operation is completed. → The Web API sends the result and any associated callback functions to the event loop. → The event loop then checks if the call stack is empty. → If it is, the callback is pushed onto the call stack and executed. - → If the call stack is not empty, the callback is added to the callback queue to be executed later.
  • 33. Non Blocking Techniques of Js. (Example- 4) function getData(url, callback) { const xhr = new XMLHttpRequest(); xhr.open('GET', url); xhr.onload = function() { if (xhr.status === 200) { callback(null, xhr.response); } else { callback(xhr.status); } }; xhr.onerror = function() { callback('Error'); }; xhr.send(); } getData('https://example.com/api/data', function(err, data) { if (err) { console.log('Error: ' + err); } else { console.log('Data: ' + data); } }); console.log(“Hello World!”); setTimeOut(()=>{ console.log(“after some timeout”); },5000)
  • 34. w Call Stack getData(); Display Window function getData(url, callback) { const xhr = new XMLHttpRequest(); xhr.open('GET', url); xhr.onload = function() { if (xhr.status === 200) { callback(null, xhr.response); } else { callback(xhr.status); } }; xhr.onerror = function() { callback('Error'); }; xhr.send(); } getData('https://example.com/api/data', function(err, data) { if (err) { console.log('Error: ' + err); } else { console.log('Data: ' + data); } }); console.log(“Hello World!”); setTimeOut(()=>{ console.log(“after some timeout”); },5000) p2 p1 Callback Queue Microtask Queue WEB API
  • 35. w Call Stack getData(); Display Window function getData(url, callback) { const xhr = new XMLHttpRequest(); xhr.open('GET', url); xhr.onload = function() { if (xhr.status === 200) { callback(null, xhr.response); } else { callback(xhr.status); } }; xhr.onerror = function() { callback('Error'); }; xhr.send(); } getData('https://example.com/api/data', function(err, data) { if (err) { console.log('Error: ' + err); } else { console.log('Data: ' + data); } }); console.log(“Hello World!”); setTimeOut(()=>{ console.log(“after some timeout”); },5000) p2 p1 Callback Queue Microtask Queue WEB API xhr.open('GET', url);
  • 36. w Call Stack Display Window function getData(url, callback) { const xhr = new XMLHttpRequest(); xhr.open('GET', url); xhr.onload = function() { if (xhr.status === 200) { callback(null, xhr.response); } else { callback(xhr.status); } }; xhr.onerror = function() { callback('Error'); }; xhr.send(); } getData('https://example.com/api/data', function(err, data) { if (err) { console.log('Error: ' + err); } else { console.log('Data: ' + data); } }); console.log(“Hello World!”); setTimeOut(()=>{ console.log(“after some timeout”); },5000) p2 p1 Callback Queue Microtask Queue WEB API xhr.open('GET', url);
  • 37. w Call Stack Hello World! Display Window function getData(url, callback) { const xhr = new XMLHttpRequest(); xhr.open('GET', url); xhr.onload = function() { if (xhr.status === 200) { callback(null, xhr.response); } else { callback(xhr.status); } }; xhr.onerror = function() { callback('Error'); }; xhr.send(); } getData('https://example.com/api/data', function(err, data) { if (err) { console.log('Error: ' + err); } else { console.log('Data: ' + data); } }); console.log(“Hello World!”); setTimeOut(()=>{ console.log(“after some timeout”); },5000) p2 p1 Callback Queue Microtask Queue WEB API xhr.open('GET', url); console.log(“Hel lo World!”)
  • 38. w Call Stack Hello World! Display Window function getData(url, callback) { const xhr = new XMLHttpRequest(); xhr.open('GET', url); xhr.onload = function() { if (xhr.status === 200) { callback(null, xhr.response); } else { callback(xhr.status); } }; xhr.onerror = function() { callback('Error'); }; xhr.send(); } getData('https://example.com/api/data', function(err, data) { if (err) { console.log('Error: ' + err); } else { console.log('Data: ' + data); } }); console.log(“Hello World!”); setTimeOut(()=>{ console.log(“after some timeout”); },5000) p2 p1 Callback Queue Microtask Queue WEB API xhr.open('GET', url); timeOut()
  • 39. Case -1 : Promise Resolve first than setTimeOut w Call Stack Hello World! <Some XHR Data> Data Display Window p2 p1 Callback Queue Microtask Queue WEB API timeOut() xhr.open('GET', url);
  • 40. Case -2 : Promise Resolve after setTimeOut w Call Stack Hello World! after some timeout Display Window p2 p1 Callback Queue Microtask Queue WEB API timeOut() xhr.open('GET', url);
  • 41. Case -3 : Promise Resolve together with setTimeOut w Call Stack Hello World! Display Window p2 p1 Callback Queue WEB API timeOut() xhr.open('GET', url); Microtask Queue
  • 42. Case -3 : Promise Resolve together with setTimeOut w Call Stack Hello World! <Some XHR Data> Data Display Window p2 p1 Callback Queue WEB API timeOut() Microtask Queue
  • 43. Case -3 : Promise Resolve together with setTimeOut w Call Stack Hello World! <Some XHR Data> Data after some timeout Display Window p2 p1 Callback Queue WEB API Microtask Queue