Skip to main content

Featured Post

Microtask And Macrotask in JavaScript (Finally Explained Simply)

Microtask And Macrotask in JavaScript (Finally Explained Simply)

 Microtask And Macrotask in JavaScript (Finally Explained Simply)

JavaScript looks simple, but sometimes its working becomes difficult to understand.

You write some code, and the you see a output in an order you didn’t expect. This confuses many beginners and even experienced developers.

The reason behind this is how JavaScript handles tasks in the background.

JavaScript does not run everything at the same time. It decides what to run now and what to run later.

This is where Microtasks and Macrotasks come in.

What Microtasks and Macrotasks are?

A Macrotask is a big task that JavaScript runs one by one.

Example of microtask are setTimeout,setInterval,UI events,setImmediate 

Microtask And Macrotask in JavaScript (Finally Explained Simply)


What is a Microtask?

A Microtask is a small but very important task.
Example of microtask are Promise.then ,
Promise.catch ,Promise.finally and queue Microtask
Microtask And Macrotask in JavaScript (Finally Explained Simply)



Important:All microtasks run before any macrotask
Microtask And Macrotask in JavaScript (Finally Explained Simply)


Output 
Microtask And Macrotask in JavaScript (Finally Explained Simply)





Step by step: Start-->runs immediately 

 Set timeout --> goes to macrotask queue

 Promise.then--> goes to microtask queue

 End --> runs immediately JavaScript finishes normal code JavaScript runs all microtasks JavaScript runs one macrotask



Summary:In JavaScript, microtasks are executed first. Then macrotasks are executed after all microtasks are completed. This helps JavaScript work smoothly.









Comments

Popular posts from this blog

How to Use Codeium for Faster Coding in VS Code (Beginner Guide 2025)

    How to Use Codeium for Faster Coding in VS Code (Beginner Guide 2025) Codeium is an AI code  assistant that helps developers by providing smart code suggestions and autocompletion as you type. It’s like having a coding partner   inside your IDE.   In this blog article I also tell  how to install codeium in vs code  features of codeium  1. codeium help us in autocompletion  it suggests us code while typing completion as you type  2.you  can type comments the code needed  it will  Automatically generated by the codeium. 3.codeium  can support in 70+ programming  languages like  c,c++,java and python  go and many more  4.you can  integrate in your code editor available  as a plugin for vs code and jetbrains jupyter lab and more  5  codeium is free for individual coders  and developers   How to Install Codeium in VS Code 1.Open  VS Code 2.Go to  Ext...

12 Beginner Mistakes and Errors in JavaScript You Should Know

 12 Beginner Mistakes and Errors in JavaScript You Should Know Friends, we all know JavaScript is a very popular programming language for developing the website and the web app. However, learning JavaScript as a beginner can be difficult, friends. We will have find some of the most common problems beginners face in JavaScript and how to solve them. 1. Understanding Variables and Data Types Friends, one of the first things when you learn the JavaScript variables . Variables are used to store information. The beginner coder many times confused in different data types such as number, string and Boolean. For example, if you try to add a number and a string, you may get an unexpected result: I will take this following example let result = 1 + "1"; Output of this program is 11 not 2 Tip: Friends, always make sure you know the type of data you are working with. You can use the typeof keyword to check. 2. Forgetting to Declare Variables Friends, in JavaScript we use let, const a...

Understanding the Difference Between HTML, CSS, and JavaScript

Understanding the Difference Between HTML, CSS, and JavaScript Friends, the websites that we use daily are made from many technologies , but today in this, we talk about three languages: HTML, CSS, and JavaScript . Each language is used for different functions. Let’s see their uses and the difference between HTML, CSS, and JavaScript. Friends, in web development, these three languages play an important role in building websites. They have combined to make every website fully functional and attractive. So, let's start knowing the difference between them. Friends, Start With First: HTML HTML is HyperText Markup Language Friends , it is the base of all websites. Think of HTML as the skeleton of a web page . It defines the structure and the content Importance of HTML It gives structure to a web page, friends HTML has tags like h1, p, a, div, etc It tells the browser this is a heading, this is a paragraph, here’s an image, and so on It does not deal with design or behavior Exa...