Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 9

JavaScript Execution:

Synchronous and
Asynchronous

JavaScript execution can be either synchronous or asynchronous. Synchronous


code runs in a linear, step-by-step fashion, while asynchronous code allows the
program to continue executing other tasks while waiting for a response.
Synchronous Execution: Understanding
the Call Stack
Call Stack
• Every Browser has JS enginee ,inside the enginee
there will a call stack
• When a function is called, it is added to the
call stack and executed.
Gobal Execution Context
• Whenever the call stack runs a code before it will
create GEC to execute a piece of code
• It is created automatically when there is a code to
execute
Characteristics of Synchronous
Operations
1 Blocking
The execution of the next line of code is blocked until the current line completes.

2 Predictable
The order of execution is predictable and easy to follow.

3 Sequential
Each operation waits for the previous one to finish.
Asynchronous Operations
1 CallStack 2 Call Back Queue 3 Event Loop

CallStack will not wait if The callback queue is a data structure that Event loop takes the next
there is any delay in the holds callback functions waiting to be callback from the callback
code. executed. These callbacks are added to the queue and pushes it onto
queue when an asynchronous operation the call stack for execution.
completes.
Characteristics of Asynchronous Operations

Non-blocking: Concurrency: Event-Driven:


Tasks can continue to run in Multiple operations can be Code execution is often
the background, allowing initiated and executed in triggered by events or the
other code to execute parallel. completion of tasks. tasks
simultaneously. grows.
Call Backs and Promises:

Call Backs: Avoiding Callback Hell Promises

Callbacks are functions passed Promises help to avoid the Promises can be chained
as arguments to other functions "callback hell" problem by together to create complex
and executed once the providing a cleaner and more asynchronous flows, making the
asynchronous operation readable syntax for code more maintainable and
completes. asynchronous code. easier to understand.
Async/Await: Syntactical Sugar for Promises

Syntactical Sugar Simplified Control Flow Improved Error Handling


Async/await is a syntactical sugar Async/await allows developers to
on top of Promises, making write asynchronous code that Async/await makes it easier to
asynchronous code more readable looks and behaves more like handle errors in asynchronous
and easier to manage. synchronous code. code, with the ability to use try-
catch blocks.
Conclusion: Mastering
Synchronous and Asynchronous
JavaScript
By understanding the differences between synchronous and asynchronous
JavaScript, and the various tools available for handling asynchronous behavior
(callbacks, promises, and async/await), developers can write more efficient,
performant, and maintainable JavaScript code.

You might also like