<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Shivam Yadav</title>
    <description>The latest articles on DEV Community by Shivam Yadav (@shivam_yadav_8e22bf5bf987).</description>
    <link>https://dev.to/shivam_yadav_8e22bf5bf987</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3799641%2Fe1602dec-3997-4bdc-be38-7884d78bde4f.png</url>
      <title>DEV Community: Shivam Yadav</title>
      <link>https://dev.to/shivam_yadav_8e22bf5bf987</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/shivam_yadav_8e22bf5bf987"/>
    <language>en</language>
    <item>
      <title>Call me back its a promise</title>
      <dc:creator>Shivam Yadav</dc:creator>
      <pubDate>Wed, 06 May 2026 20:41:22 +0000</pubDate>
      <link>https://dev.to/shivam_yadav_8e22bf5bf987/call-me-back-its-a-promise-57ia</link>
      <guid>https://dev.to/shivam_yadav_8e22bf5bf987/call-me-back-its-a-promise-57ia</guid>
      <description>&lt;h1&gt;
  
  
  hey its me shivam
&lt;/h1&gt;

&lt;p&gt;todays topic of blog is callback and promise&lt;/p&gt;

&lt;p&gt;so lets start without wasting any time lets move to actual work&lt;/p&gt;




&lt;h1&gt;
  
  
  soooo the story starts from two things
&lt;/h1&gt;

&lt;ol&gt;
&lt;li&gt;async operation&lt;/li&gt;
&lt;li&gt;sync operation&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;before understanding callback and promise we first need to understand why async programming even exist&lt;/p&gt;

&lt;p&gt;because if u directly jump into promise and callback then trust me u will memorize it&lt;/p&gt;

&lt;p&gt;not understand it 😭&lt;/p&gt;




&lt;h1&gt;
  
  
  Sync Operation
&lt;/h1&gt;

&lt;p&gt;so in this the code run line by line yes this is the simplest explanation of sync operation&lt;/p&gt;

&lt;p&gt;for example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Start&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Task 1&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Task 2&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;End&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;so the output will be&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Start
Task 1
Task 2
End
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and execution might look like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Start
 ↓
Task 1
 ↓
Task 2
 ↓
End
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;JavaScript waits for one task to finish before moving to the next.&lt;/p&gt;

&lt;p&gt;simple&lt;/p&gt;

&lt;p&gt;clean&lt;/p&gt;

&lt;p&gt;straight&lt;/p&gt;

&lt;p&gt;like a good boy following rules 😭&lt;/p&gt;




&lt;h1&gt;
  
  
  The Problem With Synchronous Code
&lt;/h1&gt;

&lt;p&gt;now the problem is imagine a heavy task&lt;/p&gt;

&lt;p&gt;like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Start&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="nx"&gt;e9&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Done&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;JavaScript is kinda blocked after start because the loop is kinda like infinite&lt;/p&gt;

&lt;p&gt;so at this time:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;UI might freeze&lt;/li&gt;
&lt;li&gt;button don't work&lt;/li&gt;
&lt;li&gt;server cannot handle other request&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;this is called blocking code&lt;/p&gt;

&lt;p&gt;and this is dangerous in backend too&lt;/p&gt;

&lt;p&gt;because one blocked request can slow other users request too&lt;/p&gt;

&lt;p&gt;and users nowadays have patience level of goldfish 💀&lt;/p&gt;




&lt;h1&gt;
  
  
  Async Operation
&lt;/h1&gt;

&lt;p&gt;now comes the async operation in this&lt;/p&gt;

&lt;p&gt;in this the code start executing line by line and whenever the heavy code like this is seen then it is executed but rather than standing there as a dumb&lt;/p&gt;

&lt;p&gt;the JavaScript will run another task after that line&lt;/p&gt;

&lt;p&gt;and when the execution of heavy task is done then the output is thrown out&lt;/p&gt;

&lt;p&gt;example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Start&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nf"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Timer Finished&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="mi"&gt;2000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;End&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;output&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Start
End
Timer Finished
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Execution flow:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Start
 ↓
setTimeout registered
 ↓
End
 ↓ (after 2 sec)
Timer Finished
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;so basically sync code is like good boy walking straight forward&lt;/p&gt;

&lt;p&gt;and async code is like:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"i will not wait i busy (he not) call me when the task is done till then i will execute another baddies"&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;😭&lt;/p&gt;




&lt;h1&gt;
  
  
  Why Async Code Exists in Node.js
&lt;/h1&gt;

&lt;p&gt;Node.js is built around:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;non-blocking architecture&lt;/li&gt;
&lt;li&gt;asynchronous execution&lt;/li&gt;
&lt;li&gt;handling multiple request together&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Imagine if every API request blocked the server.&lt;/p&gt;

&lt;p&gt;then server would literally die handling large traffic&lt;/p&gt;

&lt;p&gt;instead Node.js says:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"start the task and call me later when done"&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;that is the whole philosophy behind async programming&lt;/p&gt;




&lt;h1&gt;
  
  
  now i will explain callback and promise
&lt;/h1&gt;

&lt;p&gt;i know i know it was lengthy but it was necessary guy i cannot do anything&lt;/p&gt;

&lt;p&gt;sooooooo..........&lt;/p&gt;

&lt;p&gt;let understand&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;callback&lt;/li&gt;
&lt;li&gt;promise&lt;/li&gt;
&lt;/ol&gt;




&lt;h1&gt;
  
  
  What is Callback?
&lt;/h1&gt;

&lt;p&gt;callback is a function which is passed as a parameter into another function so that it can call it later&lt;/p&gt;

&lt;p&gt;yes this what callback is&lt;/p&gt;

&lt;p&gt;this how it look's like&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;processUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Shivam&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="nf"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;processUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Hello Shivam
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h1&gt;
  
  
  What Happened Here?
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;processUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We passed the &lt;code&gt;greet&lt;/code&gt; function as an argument.&lt;/p&gt;

&lt;p&gt;Inside:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The callback function gets executed later.&lt;/p&gt;

&lt;p&gt;that is literally callback&lt;/p&gt;

&lt;p&gt;a function waiting for another function to call it later&lt;/p&gt;

&lt;p&gt;like me waiting for motivation to study 😭&lt;/p&gt;




&lt;h1&gt;
  
  
  Callback Execution Chain
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Start Async Task
        ↓
Continue Other Code
        ↓
Task Completes Later
        ↓
Callback Added To Queue
        ↓
Event Loop Executes Callback
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;this is where Node.js event loop also enters into picture&lt;/p&gt;

&lt;p&gt;because callback don't magically execute&lt;/p&gt;

&lt;p&gt;event loop handles that execution&lt;/p&gt;

&lt;p&gt;but event loop itself is another huge topic 😭&lt;/p&gt;




&lt;h1&gt;
  
  
  Callback Hell
&lt;/h1&gt;

&lt;p&gt;while learning about callback i got to know callback hell&lt;/p&gt;

&lt;p&gt;like this not seen much nowadays but it exist&lt;/p&gt;

&lt;p&gt;so what is callback hell&lt;/p&gt;

&lt;p&gt;imagine multiple task are depended on each other&lt;/p&gt;

&lt;p&gt;example&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Get user&lt;/li&gt;
&lt;li&gt;Fetch posts&lt;/li&gt;
&lt;li&gt;Fetch comments&lt;/li&gt;
&lt;li&gt;Save logs&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Using callbacks:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;getUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;getPosts&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;posts&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;getComments&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;posts&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;comments&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nf"&gt;saveData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;comments&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Done&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="p"&gt;});&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h1&gt;
  
  
  The Callback Hell Problem
&lt;/h1&gt;

&lt;p&gt;This structure creates:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Deep nesting&lt;/li&gt;
&lt;li&gt;Poor readability&lt;/li&gt;
&lt;li&gt;Difficult debugging&lt;/li&gt;
&lt;li&gt;Complex maintenance&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is famously called:&lt;/p&gt;

&lt;h2&gt;
  
  
  Callback Hell
&lt;/h2&gt;

&lt;p&gt;or&lt;/p&gt;

&lt;h2&gt;
  
  
  Pyramid of Doom
&lt;/h2&gt;

&lt;p&gt;because code shifts endlessly to the right.&lt;/p&gt;

&lt;p&gt;bro this code literally starts looking like staircase 😭&lt;/p&gt;




&lt;h1&gt;
  
  
  So To Solve This Problem We Got Promise
&lt;/h1&gt;

&lt;p&gt;so what is promise&lt;/p&gt;

&lt;p&gt;promise is a special object used to handle asynchronous operation&lt;/p&gt;

&lt;p&gt;it represent the eventual result of an operation that may:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;complete&lt;/li&gt;
&lt;li&gt;fail&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;in the future&lt;/p&gt;

&lt;p&gt;it can be:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;API request&lt;/li&gt;
&lt;li&gt;DB calls&lt;/li&gt;
&lt;li&gt;file reading&lt;/li&gt;
&lt;li&gt;network communication&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;etc&lt;/p&gt;

&lt;p&gt;A promise acts as a placeholder for a value that is not available immediately but will be available later.&lt;/p&gt;




&lt;h1&gt;
  
  
  Why Promises Are Needed?
&lt;/h1&gt;

&lt;p&gt;JavaScript performs many operations asynchronously, such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;API requests&lt;/li&gt;
&lt;li&gt;Database queries&lt;/li&gt;
&lt;li&gt;File reading&lt;/li&gt;
&lt;li&gt;Timers&lt;/li&gt;
&lt;li&gt;Network communication&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These operations take time to complete.&lt;/p&gt;

&lt;p&gt;Instead of blocking the execution of the program while waiting, JavaScript continues executing other code and uses promises to handle the result later.&lt;/p&gt;

&lt;p&gt;Promises basically made async code cleaner and more manageable&lt;/p&gt;

&lt;p&gt;because callback hell was becoming a horror movie 💀&lt;/p&gt;




&lt;h1&gt;
  
  
  States of a Promise
&lt;/h1&gt;

&lt;p&gt;A promise can exist in one of three states:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;State&lt;/th&gt;
&lt;th&gt;Meaning&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Pending&lt;/td&gt;
&lt;td&gt;Initial state; operation is still running&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Fulfilled&lt;/td&gt;
&lt;td&gt;Operation completed successfully&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Rejected&lt;/td&gt;
&lt;td&gt;Operation failed&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h1&gt;
  
  
  State Transition
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Pending
   ↓
Fulfilled  OR  Rejected
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once fulfilled or rejected, the promise becomes permanently settled and its state cannot change again.&lt;/p&gt;




&lt;h1&gt;
  
  
  Promise Resolution
&lt;/h1&gt;

&lt;p&gt;A promise produces a result through:&lt;/p&gt;

&lt;h2&gt;
  
  
  resolve()
&lt;/h2&gt;

&lt;p&gt;Used when the operation succeeds.&lt;/p&gt;

&lt;h2&gt;
  
  
  reject()
&lt;/h2&gt;

&lt;p&gt;Used when the operation fails.&lt;/p&gt;




&lt;h1&gt;
  
  
  Example of Promise
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;fetchData&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;User data&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="mi"&gt;2000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;the promise return an object which will be having:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;state&lt;/li&gt;
&lt;li&gt;returned output(data)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;so to handle that we use &lt;code&gt;.then().catch()&lt;/code&gt; syntax&lt;/p&gt;

&lt;p&gt;which looks like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;fetchData&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;})&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h1&gt;
  
  
  Promise Lifecycle Flow
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Promise Created
        ↓
Pending
   ↓         ↓
Resolved   Rejected
   ↓           ↓
.then()     .catch()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h1&gt;
  
  
  Promise Chaining
&lt;/h1&gt;

&lt;p&gt;but here there is another problem like callback hell&lt;/p&gt;

&lt;p&gt;that is promise chain&lt;/p&gt;

&lt;p&gt;so in this the promise is chained with &lt;code&gt;.then()&lt;/code&gt; function&lt;/p&gt;

&lt;p&gt;like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;fetchUser&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;fetchPosts&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;})&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;posts&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;fetchComments&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;posts&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;})&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;comments&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;comments&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;})&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;this is still way cleaner than callback hell&lt;/p&gt;

&lt;p&gt;because now code moves downward instead of becoming pyramid&lt;/p&gt;

&lt;p&gt;which means:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;readability improve&lt;/li&gt;
&lt;li&gt;debugging improve&lt;/li&gt;
&lt;li&gt;maintenance improve&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;and developer cry little less 😭&lt;/p&gt;




&lt;h1&gt;
  
  
  Important Clarification
&lt;/h1&gt;

&lt;p&gt;Promises do NOT make code asynchronous.&lt;/p&gt;

&lt;p&gt;Node.js async APIs already work asynchronously.&lt;/p&gt;

&lt;p&gt;Promises simply provide:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Better structure&lt;/li&gt;
&lt;li&gt;Better readability&lt;/li&gt;
&lt;li&gt;Better async management&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;this is very important thing&lt;/p&gt;

&lt;p&gt;many beginner think promises create async behavior&lt;/p&gt;

&lt;p&gt;NO&lt;/p&gt;

&lt;p&gt;they just manage async behavior better&lt;/p&gt;




&lt;h1&gt;
  
  
  under the pant thing
&lt;/h1&gt;

&lt;p&gt;😭&lt;/p&gt;




&lt;h1&gt;
  
  
  Async/Await Comes from Promises
&lt;/h1&gt;

&lt;p&gt;Modern JavaScript uses:&lt;/p&gt;

&lt;h2&gt;
  
  
  async/await
&lt;/h2&gt;

&lt;p&gt;But internally:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;async/await&lt;/code&gt; is built on top of promises.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;readFile&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;readFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;demo.txt&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;utf-8&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This looks synchronous but still works asynchronously underneath.&lt;/p&gt;

&lt;p&gt;and this is why async/await became so popular&lt;/p&gt;

&lt;p&gt;because human brain likes readable code 😭&lt;/p&gt;




&lt;h1&gt;
  
  
  Callback vs Promise Readability
&lt;/h1&gt;

&lt;h2&gt;
  
  
  Callback
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;getUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;getPosts&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;posts&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;getComments&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;posts&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;comments&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;comments&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Promise
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;fetchUser&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;fetchPosts&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;posts&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;fetchComments&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;posts&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;comments&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;comments&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Promise version clearly looks cleaner&lt;/p&gt;

&lt;p&gt;less nesting&lt;/p&gt;

&lt;p&gt;less chaos&lt;/p&gt;

&lt;p&gt;less depression 💀&lt;/p&gt;




&lt;h1&gt;
  
  
  The Evolution of Async JavaScript
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Synchronous Code
        ↓
Callbacks
        ↓
Promises
        ↓
Async/Await
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h1&gt;
  
  
  Final Understanding
&lt;/h1&gt;

&lt;p&gt;Callbacks introduced asynchronous programming in Node.js by allowing functions to execute later after tasks completed.&lt;/p&gt;

&lt;p&gt;But nested callbacks created readability and maintenance problems.&lt;/p&gt;

&lt;p&gt;Promises improved async programming by providing:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Cleaner structure&lt;/li&gt;
&lt;li&gt;Easier chaining&lt;/li&gt;
&lt;li&gt;Better error handling&lt;/li&gt;
&lt;li&gt;Improved readability&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The core idea behind all of them remains the same:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Start task
      ↓
Continue execution
      ↓
Handle result later
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That non-blocking philosophy is what makes Node.js powerful for modern backend applications.&lt;/p&gt;




&lt;h1&gt;
  
  
  in the end
&lt;/h1&gt;

&lt;p&gt;so in the end this is my bye&lt;/p&gt;

&lt;p&gt;good night if u are reading this at night because i am writing this at night&lt;/p&gt;

&lt;p&gt;and if u understood callback and promise after reading this&lt;/p&gt;

&lt;p&gt;then my mission successful 😭&lt;/p&gt;

&lt;p&gt;bye bye&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>programming</category>
      <category>javascript</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Understanding React Virtual DOM Internally</title>
      <dc:creator>Shivam Yadav</dc:creator>
      <pubDate>Wed, 06 May 2026 10:20:26 +0000</pubDate>
      <link>https://dev.to/shivam_yadav_8e22bf5bf987/understanding-react-virtual-dom-internally-1hjd</link>
      <guid>https://dev.to/shivam_yadav_8e22bf5bf987/understanding-react-virtual-dom-internally-1hjd</guid>
      <description>&lt;p&gt;hey reader its me shivam&lt;/p&gt;

&lt;p&gt;and today i am all researching about DOM and how React works, why am i learning it, what benefit do i get after learning it&lt;/p&gt;

&lt;p&gt;so with this many thoughts i really got to know about what is Virtual DOM and how things internally work in React&lt;/p&gt;

&lt;p&gt;so i am not going to bore u or anything so lets start&lt;/p&gt;




&lt;h1&gt;
  
  
  First let see the problem statement
&lt;/h1&gt;

&lt;p&gt;so population is growing ( people are doing too much sex )&lt;/p&gt;

&lt;p&gt;as well the user of web has also grown&lt;/p&gt;

&lt;p&gt;and in web application there is constant changes happening like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;notification&lt;/li&gt;
&lt;li&gt;message loading&lt;/li&gt;
&lt;li&gt;forms validation&lt;/li&gt;
&lt;li&gt;comments&lt;/li&gt;
&lt;li&gt;likes&lt;/li&gt;
&lt;li&gt;realtime updates&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;etc&lt;/p&gt;

&lt;p&gt;if browser update the page directly for every changes the application would become slow and kinda heavy&lt;/p&gt;

&lt;p&gt;this is exactly the problem faced by facebook developer&lt;/p&gt;

&lt;p&gt;so they created REACT and react's virtual DOM was designed to solve this problem&lt;/p&gt;

&lt;p&gt;now this was the story for React that u have heard many time&lt;/p&gt;

&lt;p&gt;now lets start understanding of REACT working&lt;/p&gt;




&lt;h1&gt;
  
  
  The Problem : Direct DOM Manipulation is Expensive
&lt;/h1&gt;

&lt;p&gt;before understanding Virtual DOM, first lets understand Real DOM&lt;/p&gt;




&lt;h1&gt;
  
  
  what is real DOM
&lt;/h1&gt;

&lt;p&gt;the real DOM is the browser tree structure representing the HTML in element&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;div&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;h1&amp;gt;&lt;/span&gt;Hello&lt;span class="nt"&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;button&amp;gt;&lt;/span&gt;Click&lt;span class="nt"&gt;&amp;lt;/button&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Browser converts this into a tree-like structure:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;div
 ├── h1
 └── button
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The browser basically create a tree from your HTML&lt;/p&gt;

&lt;p&gt;and every single element becomes a node inside memory&lt;/p&gt;

&lt;p&gt;the issue is:&lt;/p&gt;

&lt;p&gt;updating the real DOM is Expensive ( now this sound out of the box but it is Expensive )&lt;/p&gt;

&lt;p&gt;things like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;repainting&lt;/li&gt;
&lt;li&gt;reflow&lt;/li&gt;
&lt;li&gt;layout recalculation&lt;/li&gt;
&lt;li&gt;css recalculation&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Even changing a small thing in HTML can cause heavy loading&lt;/p&gt;

&lt;p&gt;see for example there are 100's of node/html element now u want to add one more element at 98&lt;/p&gt;

&lt;p&gt;now what browser DOM will do?&lt;/p&gt;

&lt;p&gt;he will just reload everything create a new DOM tree and while creating it will add the new added element at 98'th place&lt;/p&gt;

&lt;p&gt;as u can see the unnecessary loading of all 97 element is doing&lt;/p&gt;

&lt;p&gt;parsing of HTML, then css calculation, applying&lt;/p&gt;

&lt;p&gt;it sound heavy then imagine the actual implementation&lt;/p&gt;

&lt;p&gt;and yes browser is fast...&lt;/p&gt;

&lt;p&gt;but modern UI are even faster 💀&lt;/p&gt;




&lt;h1&gt;
  
  
  this is where React enters into the picture
&lt;/h1&gt;

&lt;p&gt;imagine a social media feed&lt;/p&gt;

&lt;p&gt;every second:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;likes increase&lt;/li&gt;
&lt;li&gt;message arrive&lt;/li&gt;
&lt;li&gt;notification popup&lt;/li&gt;
&lt;li&gt;comments loading&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;are happening now with each updating, performance would collapse&lt;/p&gt;

&lt;p&gt;React solve this by:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;creating a Virtual DOM tree&lt;/li&gt;
&lt;li&gt;comparing changes effectively&lt;/li&gt;
&lt;li&gt;updating only changed node&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;yes i know sound technical but lets see something here&lt;/p&gt;




&lt;h1&gt;
  
  
  What is Virtual DOM?
&lt;/h1&gt;

&lt;p&gt;As the name implies, the Virtual DOM is a much lighter replica of the actual DOM in the form of objects.&lt;/p&gt;

&lt;p&gt;it is basically a lightweight JavaScript representation of UI&lt;/p&gt;

&lt;p&gt;this is how the object looks like&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;div&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;children&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;h1&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;props&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="na"&gt;children&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;]&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The virtual DOM can be saved in the browser memory and doesn’t directly change what is shown on the user’s browser.&lt;/p&gt;

&lt;p&gt;Implemented by several other frontend frameworks, like Vue, React’s declarative approach is unique.&lt;/p&gt;

&lt;p&gt;this is the cool part&lt;/p&gt;

&lt;p&gt;nothing is directly touching the browser DOM instantly&lt;/p&gt;

&lt;p&gt;React first prepares everything virtually&lt;/p&gt;

&lt;p&gt;like writing rough work before final answer 😭&lt;/p&gt;




&lt;h1&gt;
  
  
  this is what React do ( Master React )
&lt;/h1&gt;

&lt;p&gt;if u understand this u will get almost 90% of React working&lt;/p&gt;

&lt;p&gt;React is working in three stages&lt;/p&gt;




&lt;h1&gt;
  
  
  1) Rendering
&lt;/h1&gt;

&lt;p&gt;when ever a component state or props changes, React re-render the component UI to generate a new Virtual DOM tree&lt;/p&gt;

&lt;p&gt;this tree is kinda representation of UI component in plain JavaScript object&lt;/p&gt;

&lt;p&gt;it is kinda same as browser DOM but omitting the browser detail&lt;/p&gt;

&lt;p&gt;kinda help being fast&lt;/p&gt;

&lt;p&gt;things to be known:&lt;/p&gt;

&lt;p&gt;there are three DOM now&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;the previous Virtual DOM&lt;/li&gt;
&lt;li&gt;the new updated Virtual DOM&lt;/li&gt;
&lt;li&gt;the actual browser DOM&lt;/li&gt;
&lt;/ol&gt;




&lt;h1&gt;
  
  
  2) Diffing
&lt;/h1&gt;

&lt;p&gt;once the new Virtual DOM is created now React perform the diffing process&lt;/p&gt;

&lt;p&gt;it start comparing the new tree with the previous version to identify exactly which element have changed&lt;/p&gt;

&lt;p&gt;in short it find out diff between new and old Virtual DOM&lt;/p&gt;

&lt;p&gt;the comparison matches element types using keys and list item helps on quickly pinpointing changes&lt;/p&gt;

&lt;p&gt;now it will only isolate the difference, significantly minimizing the number of update needed&lt;/p&gt;




&lt;h1&gt;
  
  
  Example of Diffing
&lt;/h1&gt;

&lt;p&gt;Old tree:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;h1&amp;gt;&lt;/span&gt;0&lt;span class="nt"&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;New tree:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;h1&amp;gt;&lt;/span&gt;1&lt;span class="nt"&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;React notices:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;h1 tag is same&lt;/li&gt;
&lt;li&gt;Only text changed&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So React updates only text content.&lt;/p&gt;

&lt;p&gt;Minimal Real DOM Update&lt;/p&gt;

&lt;p&gt;Instead of rebuilding everything:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;h1&amp;gt;&lt;/span&gt;0&lt;span class="nt"&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;becomes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;h1&amp;gt;&lt;/span&gt;1&lt;span class="nt"&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Only the changed node updates.&lt;/p&gt;

&lt;p&gt;This is the key optimization.&lt;/p&gt;




&lt;h1&gt;
  
  
  Diagram : Diffing Process
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Old Virtual DOM
      ↓
Compare
      ↓
New Virtual DOM
      ↓
Find Difference
      ↓
Update Only Changed Node
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;things to be known:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;this process is also called reconciliation&lt;/li&gt;
&lt;li&gt;in simple words it does comparing two Virtual DOM trees to find what changed&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;some people think Virtual DOM itself is the magic&lt;/p&gt;

&lt;p&gt;but actually reconciliation is the real beast behind React 😭&lt;/p&gt;

&lt;p&gt;Virtual DOM is just the data structure&lt;/p&gt;

&lt;p&gt;reconciliation is the brain&lt;/p&gt;




&lt;h1&gt;
  
  
  3) Patching
&lt;/h1&gt;

&lt;p&gt;so now after knowing the changes React generate a minimal instruction and applies the changes to the actual DOM&lt;/p&gt;

&lt;p&gt;this process is known as patching&lt;/p&gt;

&lt;p&gt;things to be known:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;it is also known as commit phase&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;instead of re-rendering the entire DOM tree, only the modified parts are updated&lt;/p&gt;

&lt;p&gt;help in reducing performance overhead with full DOM updates&lt;/p&gt;

&lt;p&gt;and this is why React apps feel smooth most of the time&lt;/p&gt;

&lt;p&gt;unless u forgot dependency array in &lt;code&gt;useEffect&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;then may god help your CPU 💀&lt;/p&gt;




&lt;h1&gt;
  
  
  NOW lets deep dive in more of rendering
&lt;/h1&gt;

&lt;h1&gt;
  
  
  How re-rendering impacts performance
&lt;/h1&gt;

&lt;p&gt;DOM operations are very fast, light operations.&lt;/p&gt;

&lt;p&gt;However, when the app data changes and triggers an update, re-rendering can be expensive.&lt;/p&gt;

&lt;p&gt;Let’s simulate re-rendering a page with the JavaScript code below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;update&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;element&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;`
  &amp;lt;h3&amp;gt;JavaScript:&amp;lt;/h3&amp;gt;
  &amp;lt;form&amp;gt;
   &amp;lt;input type="text"/&amp;gt;
  &amp;lt;/form&amp;gt;
  &amp;lt;span&amp;gt;Time: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;toLocaleTimeString&lt;/span&gt;&lt;span class="p"&gt;()}&lt;/span&gt;&lt;span class="s2"&gt;&amp;lt;/span&amp;gt;
 `&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

 &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;root1&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;innerHTML&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;element&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="nf"&gt;setInterval&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;update&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The DOM tree representing the document looks like the following:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F63au0wj256onj4nak02a.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F63au0wj256onj4nak02a.png" alt=" " width="730" height="374"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;now this &lt;code&gt;setInterval()&lt;/code&gt; callback is triggering the rendering in each second as can see in the GIF&lt;/p&gt;

&lt;p&gt;so basically the DOM element are getting rebuild and painted on each update(second)&lt;/p&gt;

&lt;p&gt;and the text input in UI also loses its state due to the re-render&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fv5tu6ljqwuumfwaf8rtn.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fv5tu6ljqwuumfwaf8rtn.gif" alt=" " width="730" height="259"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;soo as u can see the problem clearly like really clearly&lt;/p&gt;

&lt;p&gt;when ever the UI update the things written on input is getting wiped out like it was never there&lt;/p&gt;

&lt;p&gt;basically the state is getting changed&lt;/p&gt;

&lt;p&gt;so to solve this problem different JS framework provide different solution&lt;/p&gt;

&lt;p&gt;React implement the concept of the Virtual DOM&lt;/p&gt;

&lt;p&gt;As the name implies, the virtual DOM is a much lighter replica of the actual DOM in the form of objects.&lt;/p&gt;

&lt;p&gt;this is how the object looks like&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6ou819t42948f026grm8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6ou819t42948f026grm8.png" alt=" " width="730" height="270"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The virtual DOM can be saved in the browser memory and doesn’t directly change what is shown on the user’s browser.&lt;/p&gt;




&lt;h1&gt;
  
  
  how React implement the Virtual DOM
&lt;/h1&gt;

&lt;p&gt;To understand the Virtual DOM strategy, we need to understand the two major phases that are involved:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;rendering&lt;/li&gt;
&lt;li&gt;reconciliation&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When we render an application UI, React creates a Virtual DOM tree representing that UI and stores it in memory.&lt;/p&gt;

&lt;p&gt;On the next update, or in other words, when the data that renders the app changes, React will automatically create a new Virtual DOM tree for the update.&lt;/p&gt;

&lt;p&gt;To further explain this, we can visually represent the Virtual DOM as follows:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyg7qbug3vtw3mjiy0857.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyg7qbug3vtw3mjiy0857.png" alt=" " width="730" height="479"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The image on the left is the initial render.&lt;/p&gt;

&lt;p&gt;As the Time changes, React creates a new tree with the updated node, as seen on the right side.&lt;/p&gt;

&lt;p&gt;Remember:&lt;/p&gt;

&lt;p&gt;the Virtual DOM is just an object representing the UI&lt;/p&gt;

&lt;p&gt;so nothing gets drawn on the screen directly.&lt;/p&gt;

&lt;p&gt;After React creates the new Virtual DOM tree, it compares it to the previous snapshot using a diffing algorithm called reconciliation to figure out what changes are necessary.&lt;/p&gt;

&lt;p&gt;After the reconciliation process, React uses a renderer library like ReactDOM, which takes the different information to update the rendered app.&lt;/p&gt;

&lt;p&gt;This library ensures that the actual DOM only receives and repaints the updated node or nodes.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F67zwevpx1put9miam6ch.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F67zwevpx1put9miam6ch.png" alt=" " width="730" height="378"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As seen in the image above, only the node whose data changes gets repainted in the actual DOM.&lt;/p&gt;

&lt;p&gt;The GIF below further proves this statement:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6dtto9nsnejm5c2of7pm.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6dtto9nsnejm5c2of7pm.gif" alt=" " width="730" height="239"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h1&gt;
  
  
  The React Diffing Process
&lt;/h1&gt;

&lt;p&gt;When React diffs two Virtual DOM trees, it begins by comparing whether or not both snapshots have the same root element.&lt;/p&gt;

&lt;p&gt;If they have the same elements, like in our case where the updated nodes are of the same &lt;code&gt;span&lt;/code&gt; element type, React moves on and checks the attributes.&lt;/p&gt;

&lt;p&gt;If attributes are same then React moves deeper into child nodes.&lt;/p&gt;

&lt;p&gt;Upon seeing that the Time text node has changed, React will only update the actual node in the real DOM.&lt;/p&gt;




&lt;h1&gt;
  
  
  Different Element Type Case
&lt;/h1&gt;

&lt;p&gt;if both snapshots have different element types, React will destroy the old DOM nodes and build a new one.&lt;/p&gt;

&lt;p&gt;For instance:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;span&amp;gt;&lt;/span&gt;Time: 04:36:35&lt;span class="nt"&gt;&amp;lt;/span&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;becomes&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;div&amp;gt;&lt;/span&gt;Time: 04:36:38&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;now React cannot optimize this because the element type itself changed&lt;/p&gt;

&lt;p&gt;so React rebuilds that section&lt;/p&gt;




&lt;h1&gt;
  
  
  Example using React State
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;open&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setOpen&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

 &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt; &lt;span class="na"&gt;className&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"App"&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
   &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt; &lt;span class="na"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;setOpen&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;prev&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;prev&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;toggle&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;

   &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt; &lt;span class="na"&gt;className&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;open&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;open&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;close&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
    I'm &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;open&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;opened&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;closed&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
   &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
 &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Updating the component state re-renders the component.&lt;/p&gt;

&lt;p&gt;However, as shown below, on every re-render, React knows only to update:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the class name&lt;/li&gt;
&lt;li&gt;the text that changed&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This update will not hurt unaffected elements in the render.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1uxyetp72jjidhs9o0oy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1uxyetp72jjidhs9o0oy.gif" alt=" " width="730" height="259"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;and this is why React feel smooth&lt;/p&gt;

&lt;p&gt;otherwise every button click would feel like windows XP restarting 😭&lt;/p&gt;




&lt;h1&gt;
  
  
  How React Diffs Lists
&lt;/h1&gt;

&lt;p&gt;When we modify a list of items, how React diffs the list depends on whether the items are added at the beginning or the end of the list.&lt;/p&gt;

&lt;p&gt;Consider this list:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;ul&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;li&amp;gt;&lt;/span&gt;item 3&lt;span class="nt"&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;li&amp;gt;&lt;/span&gt;item 4&lt;span class="nt"&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;li&amp;gt;&lt;/span&gt;item 5&lt;span class="nt"&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/ul&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;On the next update, lets append an item 6 at the end:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;ul&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;li&amp;gt;&lt;/span&gt;item 3&lt;span class="nt"&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;li&amp;gt;&lt;/span&gt;item 4&lt;span class="nt"&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;li&amp;gt;&lt;/span&gt;item 5&lt;span class="nt"&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;li&amp;gt;&lt;/span&gt;item 6&lt;span class="nt"&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/ul&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;React compares the items from the top.&lt;/p&gt;

&lt;p&gt;It matches:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;item 3&lt;/li&gt;
&lt;li&gt;item 4&lt;/li&gt;
&lt;li&gt;item 5&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;and only insert item 6&lt;/p&gt;

&lt;p&gt;This computation is straightforward for React.&lt;/p&gt;

&lt;p&gt;easy work for React&lt;/p&gt;




&lt;h1&gt;
  
  
  But now comes the real issue
&lt;/h1&gt;

&lt;p&gt;what if we prepend item 2?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;ul&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;li&amp;gt;&lt;/span&gt;item 2&lt;span class="nt"&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;li&amp;gt;&lt;/span&gt;item 3&lt;span class="nt"&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;li&amp;gt;&lt;/span&gt;item 4&lt;span class="nt"&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;li&amp;gt;&lt;/span&gt;item 5&lt;span class="nt"&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/ul&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Similarly, React compares from the top, and immediately realizes that item 3 doesn’t match item 2 of the updated tree.&lt;/p&gt;

&lt;p&gt;now React compares from top and sees:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;item 3 ≠ item 2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;so React think:&lt;/p&gt;

&lt;p&gt;"bro everything changed 💀"&lt;/p&gt;

&lt;p&gt;and it may rebuild the whole list&lt;/p&gt;




&lt;h1&gt;
  
  
  This is why React Keys Matter
&lt;/h1&gt;

&lt;p&gt;Instead of rebuilding the entire list, we want the DOM to compute minimal operations by only prepending item 2.&lt;/p&gt;

&lt;p&gt;React lets us add a &lt;code&gt;key&lt;/code&gt; prop to uniquely identify items.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;ul&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;li&lt;/span&gt; &lt;span class="na"&gt;key=&lt;/span&gt;&lt;span class="s"&gt;"3"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;item 3&lt;span class="nt"&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;li&lt;/span&gt; &lt;span class="na"&gt;key=&lt;/span&gt;&lt;span class="s"&gt;"4"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;item 4&lt;span class="nt"&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;li&lt;/span&gt; &lt;span class="na"&gt;key=&lt;/span&gt;&lt;span class="s"&gt;"5"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;item 5&lt;span class="nt"&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/ul&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;ul&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;li&lt;/span&gt; &lt;span class="na"&gt;key=&lt;/span&gt;&lt;span class="s"&gt;"2"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;item 2&lt;span class="nt"&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;li&lt;/span&gt; &lt;span class="na"&gt;key=&lt;/span&gt;&lt;span class="s"&gt;"3"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;item 3&lt;span class="nt"&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;li&lt;/span&gt; &lt;span class="na"&gt;key=&lt;/span&gt;&lt;span class="s"&gt;"4"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;item 4&lt;span class="nt"&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;li&lt;/span&gt; &lt;span class="na"&gt;key=&lt;/span&gt;&lt;span class="s"&gt;"5"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;item 5&lt;span class="nt"&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;li&lt;/span&gt; &lt;span class="na"&gt;key=&lt;/span&gt;&lt;span class="s"&gt;"6"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;item 6&lt;span class="nt"&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/ul&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With keys, React understands:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;item 2 is new&lt;/li&gt;
&lt;li&gt;item 6 is new&lt;/li&gt;
&lt;li&gt;existing items are same&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;As a result, it would work to preserve the items that are already available and add only the new items in the DOM.&lt;/p&gt;

&lt;p&gt;so only minimal updates happen&lt;/p&gt;

&lt;p&gt;this is why senior developers scream:&lt;/p&gt;

&lt;p&gt;"PUT KEYS IN LIST"&lt;/p&gt;

&lt;p&gt;now u know why 😭&lt;/p&gt;




&lt;h1&gt;
  
  
  How is Virtual DOM Different from Real DOM?
&lt;/h1&gt;

&lt;p&gt;A common misconception is that the Virtual DOM is faster than or rivals the actual DOM.&lt;/p&gt;

&lt;p&gt;However, this is untrue.&lt;/p&gt;

&lt;p&gt;In fact, the Virtual DOM’s operations support and add on to those of the actual DOM.&lt;/p&gt;

&lt;p&gt;Essentially, the Virtual DOM provides a mechanism that allows the actual DOM to compute minimal DOM operations when re-rendering the UI.&lt;/p&gt;

&lt;p&gt;For example, when an element in the real DOM is changed, the DOM will re-render the element and all of its children.&lt;/p&gt;

&lt;p&gt;When it comes to building complex web applications with a lot of interactivity and state changes, this approach is slow and inefficient.&lt;/p&gt;

&lt;p&gt;Instead, in the rendering process, React employs the concept of the Virtual DOM, which conforms with its declarative approach.&lt;/p&gt;

&lt;p&gt;Therefore, we can specify what state we want the UI to be in, after which React makes it happen.&lt;/p&gt;

&lt;p&gt;After the virtual DOM is updated, React compares it to a snapshot of the virtual DOM taken just before the update, determines what element was changed, and then updates only that element on the real DOM.&lt;/p&gt;

&lt;p&gt;This is one method the Virtual DOM employs to optimize performance.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Real DOM&lt;/th&gt;
&lt;th&gt;Virtual DOM&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Heavy&lt;/td&gt;
&lt;td&gt;Lightweight&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Direct browser updates&lt;/td&gt;
&lt;td&gt;JS object&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Expensive updates&lt;/td&gt;
&lt;td&gt;Efficient diffing&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Causes repaint/reflow&lt;/td&gt;
&lt;td&gt;Minimal updates&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Hard manual manipulation&lt;/td&gt;
&lt;td&gt;Declarative updates&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The Virtual DOM abstracts manual DOM manipulations away from the developer, helping us write more predictable code so that we can focus on creating components.&lt;/p&gt;

&lt;p&gt;Thanks to the Virtual DOM, you don’t have to worry about state transitions.&lt;/p&gt;

&lt;p&gt;Once you update the state, React ensures that the DOM matches that state.&lt;/p&gt;

&lt;p&gt;For instance, in our last example, React ensures that on every re-render, only Time gets updated in the actual DOM.&lt;/p&gt;

&lt;p&gt;Therefore, we won’t lose the value of the input field while the UI update happens.&lt;/p&gt;




&lt;h1&gt;
  
  
  Fun Fact : Virtual DOM Isn’t Always the Fastest
&lt;/h1&gt;

&lt;p&gt;I know, bold statement.&lt;/p&gt;

&lt;p&gt;But sometimes manual DOM updates are faster, especially for static-heavy pages.&lt;/p&gt;

&lt;p&gt;Libraries like Svelte skip the Virtual DOM altogether, compiling updates directly to the DOM at build time.&lt;/p&gt;

&lt;p&gt;React trades a bit of raw speed for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;developer happiness&lt;/li&gt;
&lt;li&gt;maintainability&lt;/li&gt;
&lt;li&gt;scalability&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And lets be honest&lt;/p&gt;

&lt;p&gt;we love that.&lt;/p&gt;




&lt;h1&gt;
  
  
  Think of Virtual DOM Like a Draft System
&lt;/h1&gt;

&lt;p&gt;Think of the Virtual DOM as your favorite todo app.&lt;/p&gt;

&lt;p&gt;You write down what changed.&lt;/p&gt;

&lt;p&gt;Then review and edit before committing.&lt;/p&gt;

&lt;p&gt;Only after reviewing, you update the real list.&lt;/p&gt;

&lt;p&gt;Same with React.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Draft first (Virtual DOM)&lt;/li&gt;
&lt;li&gt;Diff and plan (reconciliation)&lt;/li&gt;
&lt;li&gt;Then update reality (Real DOM)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The Virtual DOM is often credited for React’s speed, but the real hero behind the scenes is reconciliation.&lt;/p&gt;

&lt;p&gt;it’s what determines:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;what to update&lt;/li&gt;
&lt;li&gt;when to update&lt;/li&gt;
&lt;li&gt;how to sync efficiently&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A poor reconciliation strategy can significantly impact your app’s performance.&lt;/p&gt;




&lt;h1&gt;
  
  
  in the end
&lt;/h1&gt;

&lt;p&gt;is it really fast?&lt;/p&gt;

&lt;p&gt;i asked this from my senior and he said at initial level u will see it as just extra working:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;rendering&lt;/li&gt;
&lt;li&gt;diffing&lt;/li&gt;
&lt;li&gt;patching&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;but when we start talking about millions of nodes handling then the magic of React appears&lt;/p&gt;

&lt;p&gt;at small level u might not notice the difference much&lt;/p&gt;

&lt;p&gt;but at scale?&lt;/p&gt;

&lt;p&gt;that architecture matters a lot&lt;/p&gt;

&lt;p&gt;and that all from my side&lt;/p&gt;

&lt;p&gt;if u find this blog interesting and knowledgeable please feel free to drop a like and comment down what can be better&lt;/p&gt;

&lt;p&gt;i am really in need of improving&lt;/p&gt;

&lt;p&gt;bye&lt;/p&gt;

&lt;p&gt;see u in the next blog&lt;/p&gt;

&lt;p&gt;till then bye bye&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>react</category>
      <category>beginners</category>
    </item>
    <item>
      <title>URL Parameters vs Query Strings in Express.js Explained from Scratch</title>
      <dc:creator>Shivam Yadav</dc:creator>
      <pubDate>Fri, 01 May 2026 10:18:38 +0000</pubDate>
      <link>https://dev.to/shivam_yadav_8e22bf5bf987/url-parameters-vs-query-strings-in-expressjs-explained-from-scratch-30cg</link>
      <guid>https://dev.to/shivam_yadav_8e22bf5bf987/url-parameters-vs-query-strings-in-expressjs-explained-from-scratch-30cg</guid>
      <description>&lt;p&gt;hey reader hii&lt;/p&gt;

&lt;p&gt;today we are i mean to say u and me are going to talk about URL Parameters vs Query Strings in Express.js&lt;/p&gt;

&lt;p&gt;yaaa yaaa its boring but let me make it intresting for u&lt;/p&gt;




&lt;h2&gt;
  
  
  first lets start from starting
&lt;/h2&gt;

&lt;p&gt;everyone know about Tim Berners-Lee creator of URL , HTML and HTTP&lt;/p&gt;

&lt;p&gt;he created this thing mostly to share document easily to any one&lt;/p&gt;

&lt;p&gt;but we are going to focus on URL&lt;/p&gt;




&lt;h2&gt;
  
  
  why did he create URL ?
&lt;/h2&gt;

&lt;p&gt;so the problem was how will we identify the document in this www or internet world&lt;/p&gt;

&lt;p&gt;so he end up making a unique string (yes a basic developer thinking)&lt;/p&gt;




&lt;h2&gt;
  
  
  let decode how the first URL looks like
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;http://info.cern.ch/hypertext/WWW/TheProject.html&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;yes yes this is how it look like it is messy but everything is messy at first gradually it will get clean that what history sayy&lt;/p&gt;

&lt;p&gt;okay okay let come back to the main topic&lt;/p&gt;




&lt;h2&gt;
  
  
  so in this URL
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;http://&lt;br&gt;
this act as a protocol&lt;br&gt;
tell system to use this protocol to communicate with this system&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;info.cern.ch&lt;br&gt;
this is host&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;/hypertext/WWW/TheProject.html&lt;br&gt;
this is path&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;




&lt;p&gt;so if u see this carefully u can see that this kinda a looking like a file system path&lt;/p&gt;

&lt;p&gt;why ? because developer are familiar with this strcuture&lt;/p&gt;

&lt;p&gt;so he did not made something new he just reused a familiar structure&lt;/p&gt;

&lt;p&gt;but till now this URL is not having the params and query&lt;/p&gt;




&lt;h1&gt;
  
  
  Where query strings came from ?
&lt;/h1&gt;

&lt;p&gt;with development the static website get converted into a dynamic website&lt;/p&gt;

&lt;p&gt;DB gets involve, search systems came in&lt;/p&gt;

&lt;p&gt;dynamic content was needed, people want condition baised data&lt;/p&gt;




&lt;h3&gt;
  
  
  That’s where query strings appeared
&lt;/h3&gt;

&lt;p&gt;so there are many parameter in an url like path , query , hash (#) ,etc&lt;/p&gt;

&lt;p&gt;we are going to focus on path which is params and another is query&lt;/p&gt;




&lt;h1&gt;
  
  
  so lets start with params
&lt;/h1&gt;

&lt;p&gt;so this how it look in url&lt;/p&gt;

&lt;p&gt;&lt;code&gt;/users/123/posts/456&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This is not random. It follows a hierarchical structure&lt;/p&gt;

&lt;p&gt;&lt;code&gt;/resource/identifier&lt;/code&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Where did this structure come from?
&lt;/h2&gt;

&lt;p&gt;this was designed directly from file system (Unix)&lt;/p&gt;

&lt;p&gt;&lt;code&gt;/home/shivam/file.txt&lt;/code&gt;&lt;/p&gt;




&lt;p&gt;so basically it means&lt;/p&gt;

&lt;p&gt;Go inside:&lt;/p&gt;

&lt;p&gt;users&lt;br&gt;
user 123&lt;br&gt;
posts&lt;br&gt;
post 456&lt;/p&gt;


&lt;h2&gt;
  
  
  General structure pattern
&lt;/h2&gt;

&lt;p&gt;Basic form:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;/collection/:id&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Nested form:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;/collection/:id/subcollection/:subId&lt;/code&gt;&lt;/p&gt;


&lt;h2&gt;
  
  
  How Express understands this
&lt;/h2&gt;

&lt;p&gt;When you write:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/users/:id&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;params&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Internally Express converts it to:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;/users/([^/]+)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;it means match after any things comes after users/&lt;br&gt;
and till the next /&lt;/p&gt;



&lt;p&gt;BTW one more thing express does not see the :id it see what is comming next after the users/ and till the next /&lt;/p&gt;

&lt;p&gt;so :id is only showing as a wrapper code to identify it in a code so the developer can use the value in his code&lt;/p&gt;


&lt;h2&gt;
  
  
  how to use in express
&lt;/h2&gt;

&lt;p&gt;so if first thing u have to do is to pass the name of the params so that in your code u can use it for accessing it&lt;/p&gt;

&lt;p&gt;it look like this&lt;/p&gt;

&lt;p&gt;&lt;code&gt;/user/:id&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;so if u want to access it u can use&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;params&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Request:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;/users/123&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Extracted:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;params&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;123&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Multiple params structure
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/users/:userId/posts/:postId&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;...)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Request:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;/users/123/posts/456&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;123&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;postId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;456&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h1&gt;
  
  
  how lets talk about query
&lt;/h1&gt;

&lt;p&gt;so the first AIM of query is to show data to user depending on condition&lt;/p&gt;

&lt;p&gt;A query string is the part of a URL that comes after ? and is made of key=value pairs&lt;/p&gt;




&lt;h2&gt;
  
  
  this is how it looks like
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;/search?q=express&amp;amp;sort=latest&amp;amp;page=2&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;? starts the query string&lt;br&gt;
&amp;amp; separates parameters&lt;br&gt;
= assigns value to a key&lt;/p&gt;




&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;q&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;express&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;sort&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;latest&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;page&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;2&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Original purpose
&lt;/h2&gt;

&lt;p&gt;“Provide additional data to the resource without changing its identity.”&lt;/p&gt;

&lt;p&gt;That’s why&lt;/p&gt;

&lt;p&gt;&lt;code&gt;/users/123&lt;/code&gt; means the user which is identity&lt;/p&gt;

&lt;p&gt;&lt;code&gt;/users?age=20&lt;/code&gt; means filtered list which is instruction&lt;/p&gt;



&lt;p&gt;if u look closely u can the query parameter is inspired by SQL COMMAND&lt;/p&gt;

&lt;p&gt;example&lt;/p&gt;

&lt;p&gt;&lt;code&gt;SELECT * FROM users WHERE age = 20&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Query string became the web version of that&lt;/p&gt;

&lt;p&gt;&lt;code&gt;/users?age=20&lt;/code&gt;&lt;/p&gt;


&lt;h1&gt;
  
  
  4. Full structure
&lt;/h1&gt;

&lt;p&gt;General format&lt;/p&gt;

&lt;p&gt;&lt;code&gt;?key=value&amp;amp;key2=value&amp;amp;key3=value&lt;/code&gt;&lt;/p&gt;


&lt;h2&gt;
  
  
  Important rules
&lt;/h2&gt;
&lt;h3&gt;
  
  
  1. every one need starting point so as query so it becames ?
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;/users?age=20&lt;/code&gt;&lt;/p&gt;


&lt;h3&gt;
  
  
  2. there can be multiple query parameter so we use &amp;amp;
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;/users?age=20&amp;amp;active=true&lt;/code&gt;&lt;/p&gt;


&lt;h3&gt;
  
  
  3. keys should be unique
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;/users?age=20&amp;amp;age=30&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Allowed but it can create ambiguity&lt;/p&gt;


&lt;h3&gt;
  
  
  one of the most important point
&lt;/h3&gt;

&lt;p&gt;Everything is text&lt;/p&gt;

&lt;p&gt;?age=20&lt;/p&gt;

&lt;p&gt;comes as&lt;/p&gt;

&lt;p&gt;"20"&lt;/p&gt;

&lt;p&gt;not number&lt;/p&gt;


&lt;h1&gt;
  
  
  How query parsing works internally
&lt;/h1&gt;

&lt;p&gt;Step by step&lt;/p&gt;

&lt;p&gt;Split after ?&lt;/p&gt;

&lt;p&gt;age=20&amp;amp;active=true&lt;/p&gt;

&lt;p&gt;Split by &amp;amp;&lt;/p&gt;

&lt;p&gt;["age=20", "active=true"]&lt;/p&gt;

&lt;p&gt;Split by =&lt;/p&gt;

&lt;p&gt;age becomes 20&lt;br&gt;
active becomes true&lt;/p&gt;

&lt;p&gt;Create object&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;20&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;active&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;true&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h1&gt;
  
  
  In Express.js
&lt;/h1&gt;

&lt;p&gt;so in params u first have to declare the params but not in this&lt;/p&gt;

&lt;p&gt;it will directly given into the req.query so access it from there&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/users&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;query&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Request&lt;/p&gt;

&lt;p&gt;&lt;code&gt;/users?age=20&amp;amp;active=true&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Output&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;20&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;active&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;true&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Important detail
&lt;/h2&gt;

&lt;p&gt;Express uses parsers like&lt;/p&gt;

&lt;p&gt;querystring which is basic&lt;br&gt;
qs which is advanced&lt;/p&gt;


&lt;h1&gt;
  
  
  Advanced query formats
&lt;/h1&gt;
&lt;h2&gt;
  
  
  Arrays
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;/products?category=electronics&amp;amp;category=books&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;or&lt;/p&gt;

&lt;p&gt;&lt;code&gt;/products?category[]=electronics&amp;amp;category[]=books&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Parsed&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;category&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;electronics&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;books&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Nested objects
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;/user?filter[name]=shivam&amp;amp;filter[age]=20&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Parsed&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;shivam&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;20&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Boolean or numbers
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;?active=true&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Still string&lt;/p&gt;

&lt;p&gt;"true"&lt;/p&gt;

&lt;p&gt;You must convert&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;active&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;query&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;active&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;true&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h1&gt;
  
  
  Real-world use cases
&lt;/h1&gt;

&lt;h2&gt;
  
  
  Search
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;/search?q=nodejs&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Filtering
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;/products?price=low&amp;amp;brand=apple&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Pagination
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;/posts?page=2&amp;amp;limit=10&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Sorting
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;/posts?sort=latest&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Feature toggles
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;/dashboard?darkMode=true&lt;/code&gt;&lt;/p&gt;




&lt;h1&gt;
  
  
  14. Internal philosophy
&lt;/h1&gt;

&lt;p&gt;Query params follow this principle&lt;/p&gt;

&lt;p&gt;“Same resource, different representation”&lt;/p&gt;

&lt;p&gt;Example&lt;/p&gt;

&lt;p&gt;&lt;code&gt;/products?sort=price&lt;/code&gt;&lt;br&gt;
&lt;code&gt;/products?sort=rating&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Same resource but different view depending on how you request it&lt;/p&gt;




&lt;h1&gt;
  
  
  15. Final mental model
&lt;/h1&gt;

&lt;p&gt;Burn this&lt;/p&gt;

&lt;p&gt;Query means how you want the data&lt;/p&gt;

&lt;p&gt;Example&lt;/p&gt;

&lt;p&gt;&lt;code&gt;/users/123?include=posts&amp;amp;limit=5&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;/users/123&lt;/code&gt; means what resource you are targeting&lt;/p&gt;

&lt;p&gt;&lt;code&gt;?include=posts&amp;amp;limit=5&lt;/code&gt; means how you want that data to be returned&lt;/p&gt;




&lt;h1&gt;
  
  
  Final closure
&lt;/h1&gt;

&lt;p&gt;so if u look at everything from starting to now&lt;/p&gt;

&lt;p&gt;you will realize that nothing in URL design is random&lt;/p&gt;

&lt;p&gt;everything is built on simple thinking&lt;/p&gt;

&lt;p&gt;first identify the resource clearly using path&lt;/p&gt;

&lt;p&gt;then modify or filter the data using query&lt;/p&gt;

&lt;p&gt;params give you the exact thing you want&lt;/p&gt;

&lt;p&gt;query helps you control how that thing should behave&lt;/p&gt;

&lt;p&gt;this separation is the reason why APIs today are clean predictable and scalable&lt;/p&gt;

&lt;p&gt;if u mix them randomly then your API will work but it will not be understandable and not maintainable&lt;/p&gt;

&lt;p&gt;and that is the difference between just making something work and actually designing it properly&lt;/p&gt;

&lt;p&gt;so next time when you design an API do not just write routes&lt;/p&gt;

&lt;p&gt;think what is the resource and how it should be accessed&lt;/p&gt;

&lt;p&gt;that is where real backend engineering starts&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>node</category>
      <category>express</category>
      <category>ai</category>
    </item>
    <item>
      <title>What Arranged Marriage Taught Me About JavaScript Promises</title>
      <dc:creator>Shivam Yadav</dc:creator>
      <pubDate>Sun, 01 Mar 2026 08:42:21 +0000</pubDate>
      <link>https://dev.to/shivam_yadav_8e22bf5bf987/what-arranged-marriage-taught-me-about-javascript-promises-211l</link>
      <guid>https://dev.to/shivam_yadav_8e22bf5bf987/what-arranged-marriage-taught-me-about-javascript-promises-211l</guid>
      <description>&lt;h1&gt;
  
  
  Promises, Rishtas, Aur Thoda Sa Intezaar
&lt;/h1&gt;

&lt;p&gt;Har Indian ghar mein ek phase aata hai.&lt;/p&gt;

&lt;p&gt;Shaam ka time.&lt;br&gt;
Mummy chai bana rahi hoti hain.&lt;br&gt;
TV pe daily soap chal raha hota hai.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwt3vkn8xdincfnk1jmiv.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwt3vkn8xdincfnk1jmiv.jpg" alt="Image" width="450" height="350"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Aur beech mein casually ek line aati hai&lt;/p&gt;

&lt;p&gt;“Beta… Sharma ji ka phone aaya tha.”&lt;/p&gt;

&lt;p&gt;Bas.&lt;/p&gt;

&lt;p&gt;Background music change.&lt;/p&gt;

&lt;p&gt;Aapko samajh aa jaata hai.&lt;/p&gt;

&lt;p&gt;Shaadi season officially shuru ho chuka hai.&lt;/p&gt;

&lt;p&gt;Riya bhi ussi phase mein thi.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffrrh068kumvuqjanhqba.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffrrh068kumvuqjanhqba.jpg" alt="Image" width="700" height="466"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Job stable.&lt;br&gt;
Salary theek.&lt;br&gt;
Life sorted.&lt;/p&gt;

&lt;p&gt;Par ghar walon ke hisaab se—&lt;/p&gt;

&lt;p&gt;“Ab settle ho jao.”&lt;/p&gt;

&lt;p&gt;Aur yahin se shuru hoti hai Promises ki kahani.&lt;/p&gt;


&lt;h1&gt;
  
  
  Promise Kya Hota Hai?
&lt;/h1&gt;

&lt;p&gt;Promise matlab&lt;/p&gt;

&lt;p&gt;“Abhi answer nahi milega.&lt;br&gt;
Par milega.&lt;br&gt;
Ya toh haan mein milega.&lt;br&gt;
Ya seedha mana.”&lt;/p&gt;

&lt;p&gt;Beech mein koi confusion nahi.&lt;/p&gt;

&lt;p&gt;Jaise rishta.&lt;/p&gt;

&lt;p&gt;Ya pakka.&lt;br&gt;
Ya cancel.&lt;/p&gt;

&lt;p&gt;JavaScript ne is waiting ko naam diya Promise.&lt;/p&gt;


&lt;h1&gt;
  
  
  Promise.all()  “Sabki Haan Zaroori Hai”
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftd4kjfdo7bxu25iho3ef.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftd4kjfdo7bxu25iho3ef.jpg" alt="Image" width="800" height="391"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Riya ka pehla rishta aaya.&lt;/p&gt;

&lt;p&gt;Ladka achha tha.&lt;br&gt;
Salary impressive thi.&lt;br&gt;
Family decent thi.&lt;/p&gt;

&lt;p&gt;Par Indian shaadi emotional nahi hoti.&lt;/p&gt;

&lt;p&gt;System hota hai.&lt;/p&gt;

&lt;p&gt;Checklist hoti hai.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Riya ko ladka pasand?&lt;/li&gt;
&lt;li&gt;Mummy ko family pasand?&lt;/li&gt;
&lt;li&gt;Papa ko job pasand?&lt;/li&gt;
&lt;li&gt;Kundli match?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Sab green tick chahiye.&lt;/p&gt;

&lt;p&gt;Ek bhi red cross?&lt;/p&gt;

&lt;p&gt;Game over.&lt;/p&gt;

&lt;p&gt;Yeh hai &lt;code&gt;Promise.all()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Sab promises successful honge tabhi result milega.&lt;/p&gt;
&lt;h3&gt;
  
  
  Code
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;riya&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Riya agreed&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;parents&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Parents agreed&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;horoscope&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Horoscope matched&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;all&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="nx"&gt;riya&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;parents&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;horoscope&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Shaadi Pakki &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Rishta Toot Gaya &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Agar ek bhi reject ho jaye…&lt;/p&gt;

&lt;p&gt;Sab khatam.&lt;/p&gt;


&lt;h1&gt;
  
  
  Promise.allSettled() — “Mujhe Sach Batao”
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7uwunq60o7zjrlovufgb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7uwunq60o7zjrlovufgb.png" alt="Image" width="800" height="1126"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Do teen rishton ke baad Riya badal gayi.&lt;/p&gt;

&lt;p&gt;Ab woh toot’ti nahi thi.&lt;/p&gt;

&lt;p&gt;Bas bolti thi&lt;/p&gt;

&lt;p&gt;“Thik hai. Batao feedback kya hai.”&lt;/p&gt;

&lt;p&gt;Ek ne mana kiya.&lt;br&gt;
Ek ne horoscope issue bola.&lt;br&gt;
Ek ne haan bola.&lt;/p&gt;

&lt;p&gt;Pehle rejection insult lagta tha.&lt;/p&gt;

&lt;p&gt;Ab woh sirf data tha.&lt;/p&gt;

&lt;p&gt;Yeh hai &lt;code&gt;Promise.allSettled()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Yeh kehta hai—&lt;/p&gt;

&lt;p&gt;Main sabka result launga.&lt;br&gt;
Jo pass hua woh bhi.&lt;br&gt;
Jo fail hua woh bhi.&lt;/p&gt;
&lt;h3&gt;
  
  
  Code
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;boy1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Good Family&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;boy2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Horoscope Issue&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;boy3&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Nice Nature&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;allSettled&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="nx"&gt;boy1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;boy2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;boy3&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[
 { status: 'fulfilled', value: 'Good Family' },
 { status: 'rejected', reason: 'Horoscope Issue' },
 { status: 'fulfilled', value: 'Nice Nature' }
]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Maturity is when rejection becomes information.&lt;/p&gt;




&lt;h1&gt;
  
  
  Promise.race() “Jo Pehle Haan Karega”
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzci8mnsyc2so8zjcep4k.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzci8mnsyc2so8zjcep4k.jpg" alt="Image" width="300" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Ab ghar wale impatient ho gaye.&lt;/p&gt;

&lt;p&gt;Dadi ne declare kar diya—&lt;/p&gt;

&lt;p&gt;“Jo pehle haan karega, wahi final.”&lt;/p&gt;

&lt;p&gt;Do families baat kar rahi thi.&lt;/p&gt;

&lt;p&gt;Delhi wale soch rahe the.&lt;br&gt;
Mumbai wale ready ho gaye.&lt;/p&gt;

&lt;p&gt;Decision ho gaya.&lt;/p&gt;

&lt;p&gt;Yeh hai &lt;code&gt;Promise.race()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Jo pehle settle hua&lt;br&gt;
Wahi result.&lt;/p&gt;
&lt;h3&gt;
  
  
  Code
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;delhi&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
  &lt;span class="nf"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Delhi Ready&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="mi"&gt;2000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;mumbai&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
  &lt;span class="nf"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Mumbai Ready&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;race&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="nx"&gt;delhi&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;mumbai&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Speed bhi ek decision hoti hai.&lt;/p&gt;


&lt;h1&gt;
  
  
  Promise.any() “Bas Ek Haan Kaafi Hai”
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.freepixel.com%2Fpreview%2Ffree-photos-eastern-indian-woman-meditating-and-lighting-a-lamp-preview-100329668.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.freepixel.com%2Fpreview%2Ffree-photos-eastern-indian-woman-meditating-and-lighting-a-lamp-preview-100329668.jpg" alt="Image" width="800" height="448"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Phir ek phase aaya.&lt;/p&gt;

&lt;p&gt;Jab Riya ne decide kiya&lt;/p&gt;

&lt;p&gt;“Mujhe perfect nahi chahiye.”&lt;/p&gt;

&lt;p&gt;“Bas ek insaan chahiye jo mujhe choose kare.”&lt;/p&gt;

&lt;p&gt;Paanch ne mana kar diya.&lt;/p&gt;

&lt;p&gt;Ek ne haan bola.&lt;/p&gt;

&lt;p&gt;Aur us ek haan ne&lt;br&gt;
sab purane mana ko irrelevant bana diya.&lt;/p&gt;

&lt;p&gt;Yeh hai &lt;code&gt;Promise.any()&lt;/code&gt;.&lt;/p&gt;
&lt;h3&gt;
  
  
  Code
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;p1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Rejected 1&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;p2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Rejected 2&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;p3&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;He Said Yes ❤️&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;any&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="nx"&gt;p1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;p2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;p3&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Sabne Mana Kar Diya&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;One success is enough.&lt;/p&gt;


&lt;h1&gt;
  
  
  Promise.resolve() “Instant Haan”
&lt;/h1&gt;

&lt;p&gt;Kabhi kabhi cheezein simple hoti hain.&lt;/p&gt;

&lt;p&gt;Milte hi vibe match.&lt;/p&gt;

&lt;p&gt;No drama.&lt;/p&gt;

&lt;p&gt;Seedha&lt;/p&gt;

&lt;p&gt;“Let’s do this.”&lt;/p&gt;

&lt;p&gt;Yeh hota hai &lt;code&gt;Promise.resolve()&lt;/code&gt;.&lt;/p&gt;
&lt;h3&gt;
  
  
  Code Example
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Rishta Pakka &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Rishta Pakka 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Immediate success. No waiting.&lt;/p&gt;




&lt;h1&gt;
  
  
  Promise.reject()  “Seedha Mana”
&lt;/h1&gt;

&lt;p&gt;Kabhi kabhi message aata hai&lt;/p&gt;

&lt;p&gt;“Sorry. Not interested.”&lt;/p&gt;

&lt;p&gt;Bas.&lt;/p&gt;

&lt;p&gt;Koi explanation nahi.&lt;/p&gt;

&lt;p&gt;Yeh hota hai &lt;code&gt;Promise.reject()&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Code Example
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Rishta Cancelled &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Rishta Cancelled 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Immediate rejection.&lt;/p&gt;




&lt;h1&gt;
  
  
  The 1% Realization
&lt;/h1&gt;

&lt;p&gt;Zindagi synchronous nahi hoti.&lt;/p&gt;

&lt;p&gt;Har cheez time leti hai.&lt;/p&gt;

&lt;p&gt;Rishtas.&lt;br&gt;
Career.&lt;br&gt;
Dreams.&lt;br&gt;
Replies.&lt;br&gt;
Results.&lt;/p&gt;

&lt;p&gt;Aur har jagah wahi patterns milte hain—&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Sabka approval → &lt;code&gt;Promise.all()&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Full report → &lt;code&gt;Promise.allSettled()&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Jo pehle aaye → &lt;code&gt;Promise.race()&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Bas ek chance → &lt;code&gt;Promise.any()&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Programming sirf syntax nahi hai.&lt;/p&gt;

&lt;p&gt;It’s life converted into logic.&lt;/p&gt;

&lt;p&gt;Aur jab tum Promises samajh jaate ho…&lt;/p&gt;

&lt;p&gt;Tum wait karna seekh jaate ho.&lt;/p&gt;

&lt;p&gt;Kyuki intezaar bhi system ka part hai.&lt;/p&gt;

&lt;p&gt;Aur jo intezaar samajh gaya…&lt;/p&gt;

&lt;p&gt;Woh asynchronous se kabhi nahi darega.&lt;/p&gt;

</description>
      <category>chaicode</category>
      <category>webdev</category>
      <category>programming</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
