<?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: Neel-Vekariya</title>
    <description>The latest articles on DEV Community by Neel-Vekariya (@neel-vekariya).</description>
    <link>https://dev.to/neel-vekariya</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.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3968256%2Ff900b976-6eab-4295-bca9-6c8b6fd937e2.jpeg</url>
      <title>DEV Community: Neel-Vekariya</title>
      <link>https://dev.to/neel-vekariya</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/neel-vekariya"/>
    <language>en</language>
    <item>
      <title>Next step to client-side storage</title>
      <dc:creator>Neel-Vekariya</dc:creator>
      <pubDate>Sun, 23 Aug 2026 09:44:40 +0000</pubDate>
      <link>https://dev.to/neel-vekariya/next-step-to-client-side-storage-4g83</link>
      <guid>https://dev.to/neel-vekariya/next-step-to-client-side-storage-4g83</guid>
      <description>&lt;h2&gt;
  
  
  Next step to client-side storage
&lt;/h2&gt;

&lt;p&gt;In my past one blog, I wrote about how I improve the performance of the application using the local storage. And the problem local storage solves.&lt;/p&gt;

&lt;p&gt;But now I face another problem about the client storage. My project is simply about order management software for the rental clothing industry.&lt;/p&gt;

&lt;p&gt;In the rental clothing industry, Showrooms or small shops have a big problem. The problem starts when one order has a single or multiple items that are booked in a particular time range.&lt;/p&gt;

&lt;p&gt;Now, a second order wants the same item in between that particular time range. If, by mistake, the second order books that item, then the problem starts.&lt;/p&gt;

&lt;p&gt;The item is booked two times in that particular time range. That is called double booking of the item.&lt;/p&gt;

&lt;p&gt;This mistake is created by the use of traditional register booking. Now, when I need to store the items data, that is a small amount of data, so I simply use the local storage.&lt;/p&gt;

&lt;p&gt;But now I need another and a big storage for storing order details. I build two features: first one is for showing all the orders and second one is for showing the full order.&lt;/p&gt;

&lt;p&gt;To implement those features and to maintain the user experience, I decide to store a small amount of data about the order on the client side. First, I decide to store data in local storage.&lt;/p&gt;

&lt;p&gt;But to store data in the local storage is not a good option because the local storage is used for storing small details about the application, and storing order details in the local storage compromises the performance of the application.&lt;/p&gt;

&lt;p&gt;Now I want a new storage option for storing order details. And again I find out, and that is the IndexedDB.&lt;/p&gt;





&lt;p&gt;To integrate IndexedDB in my application, I want to learn about that storage. I search multiple videos about IndexedDB, but no one is teaching me properly.&lt;/p&gt;

&lt;p&gt;After finding hundreds of tutorials, I finally found one tutorial that is teaching properly how to integrate IndexedDB in the application. Now I want to share that learning with you.&lt;/p&gt;





&lt;p&gt;To integrate IndexedDB, we want to create first one database in IndexedDB. The &lt;code&gt;dbConnect&lt;/code&gt; is built for that and used to get the reference of the created database.&lt;/p&gt;

&lt;p&gt;To build that function, we want to create first two environment variables: &lt;code&gt;db_name&lt;/code&gt; and &lt;code&gt;version&lt;/code&gt;. Those two variables describe the name and the version of that database.&lt;/p&gt;

&lt;p&gt;If any updation happens in the version variable, then this function will execute and update the database.&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;dbConnect&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;req&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;indexedDB&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;db_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;version&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;onupgradeneeded&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;db&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;result&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="c1"&gt;// Access the reference of the database.&lt;/span&gt;

    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;payload&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;keyPath&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;id&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;autoIncrement&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
    &lt;span class="p"&gt;};&lt;/span&gt;

    &lt;span class="c1"&gt;// The payload is for creating a primary key.&lt;/span&gt;

    &lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createObjectStore&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;orders&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="c1"&gt;// Create the object that is called store in IndexedDB.&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;onsuccess&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="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;Success&lt;/span&gt;&lt;span class="dl"&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;result&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;onerror&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="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;Failed&lt;/span&gt;&lt;span class="dl"&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;error&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;Now, in the function, we create first one database and get the reference of that database. The &lt;code&gt;req.onupgradeneeded&lt;/code&gt; is an event for updating the database when the version variable is changed, then this event is run and updates the database.&lt;/p&gt; 

&lt;p&gt;The &lt;code&gt;req&lt;/code&gt; variable sends the request to open the database, and after the request is complete, we now have access to the database connection reference. The &lt;code&gt;payload&lt;/code&gt; is the rule for how the primary key of that particular store will be generated.&lt;/p&gt; 

&lt;p&gt;To create a store, first we access the result object, and in the result object, we use the &lt;code&gt;createObjectStore&lt;/code&gt; method. &lt;code&gt;onsuccess&lt;/code&gt; and &lt;code&gt;onerror&lt;/code&gt; are events that indicate the success or failure of the connection.&lt;/p&gt; 


 

&lt;p&gt;The function &lt;code&gt;dbConnect&lt;/code&gt; is an asynchronous function, so the next step is to convert this asynchronous function to a synchronous function. To create a synchronous function, we simply wrap this function into the promise.&lt;/p&gt;

&lt;p&gt;Let's do that.&lt;/p&gt; &lt;p&gt;We do this promise in the &lt;code&gt;req.onsuccess&lt;/code&gt; or &lt;code&gt;req.onerror&lt;/code&gt; event. If the &lt;code&gt;req&lt;/code&gt; is successful, we resolve the promise; otherwise, we reject the promise.&lt;/p&gt; 

&lt;p&gt;The updated event is:&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;onsuccess&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;resolve&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;onerror&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&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;dbConnect&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="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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;indexedDB&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;db_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;version&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;onupgradeneeded&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="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;onsuccess&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="nf"&gt;resolve&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;result&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;onerror&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="nf"&gt;reject&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;error&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;Now our function is converted into the synchronous function. Now, the next step we explore is how to store, update, read and delete the data.&lt;/p&gt; 

&lt;p&gt;In IndexedDB, all the operations are transactions. Means if any error happens, then all the started operations are undone.&lt;/p&gt; 


 

&lt;p&gt;Now the next step is to get knowledge about the permission. When we want to store or delete or update the data, the first step is to define the permission.&lt;/p&gt; &lt;p&gt;&lt;code&gt;readonly&lt;/code&gt;, &lt;code&gt;readwrite&lt;/code&gt; are the types of permission.&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;storeData&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async &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;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;db&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;dbConnect&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;transaction&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;transaction&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;orders&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;readwrite&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="c1"&gt;// Define the permission.&lt;/span&gt;

    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;payload&lt;/span&gt; &lt;span class="o"&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;Neel&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;email&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;neel@gmail.com&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
    &lt;span class="p"&gt;};&lt;/span&gt;

    &lt;span class="c1"&gt;// The data to store.&lt;/span&gt;

    &lt;span class="nx"&gt;transaction&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;objectStore&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;orders&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="nx"&gt;transaction&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;oncomplete&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="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;Data Stored Successfully.&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="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="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;error&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 is all about storing the data. Now we learn about reading the data.&lt;/p&gt; &lt;p&gt;For reading the data, we use &lt;code&gt;readonly&lt;/code&gt; permission. The &lt;code&gt;getAll()&lt;/code&gt; function is used to get all the data, and &lt;code&gt;get()&lt;/code&gt; is used to get particular data.&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;readData&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async &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;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;db&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;dbConnect&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;transaction&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;transaction&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;orders&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;readonly&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="c1"&gt;// Define the permission.&lt;/span&gt;

    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;request&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;transaction&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;objectStore&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;orders&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;getAll&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

    &lt;span class="nx"&gt;request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;onsuccess&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="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;Data Fetched Successfully.&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;request&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="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="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;error&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 is all about how to read the data. Now next, we learn how to update the data.&lt;/p&gt; &lt;p&gt;To update the data, we use the &lt;code&gt;readwrite&lt;/code&gt; permission.&lt;/p&gt;
&lt;br&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;updateData&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async &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;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;db&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;dbConnect&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;transaction&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;transaction&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;orders&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;readwrite&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="c1"&gt;// Define the permission.&lt;/span&gt;

    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;store&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;transaction&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;objectStore&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;orders&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="c1"&gt;// In this store we do not use any function; either we get the reference.&lt;/span&gt;

    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;store&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="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Get old data.&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;onsuccess&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;data&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;result&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

      &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;data&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;payload&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;data&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;Jay&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;email&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Jay@gmail.com&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
      &lt;span class="p"&gt;};&lt;/span&gt;

      &lt;span class="c1"&gt;// Only change name and email and other data store as it is.&lt;/span&gt;

      &lt;span class="nx"&gt;store&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;put&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;payload&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;error&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;error&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 is all about how to update the data. Now next, we learn how to delete the data.&lt;/p&gt; 

&lt;p&gt;To delete the data, we use the &lt;code&gt;.delete()&lt;/code&gt; method.&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;deleteData&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async &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;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;db&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;dbConnect&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;transaction&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;transaction&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;orders&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;readwrite&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="c1"&gt;// Define the permission.&lt;/span&gt;

    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;store&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;transaction&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;objectStore&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;orders&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="c1"&gt;// In this store we do not use any function; either we get the reference.&lt;/span&gt;

    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;store&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;delete&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="c1"&gt;// Delete data.&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="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;error&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;Now we successfully implemented the IndexedDB in our project.&lt;/p&gt; 

&lt;p&gt;If I made any mistake, then share my mistake in the comments.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>frontend</category>
      <category>node</category>
      <category>software</category>
    </item>
    <item>
      <title>Then My Project Needed This</title>
      <dc:creator>Neel-Vekariya</dc:creator>
      <pubDate>Sat, 15 Aug 2026 07:33:22 +0000</pubDate>
      <link>https://dev.to/neel-vekariya/then-my-project-needed-this-3fep</link>
      <guid>https://dev.to/neel-vekariya/then-my-project-needed-this-3fep</guid>
      <description>&lt;h2&gt;
  
  
  Finally, I Integrated the Multiselect Autocomplete Dropdown
&lt;/h2&gt;

&lt;p&gt;In the past few months, I learned a lot of things and also made a project after many retries. The project taught me many important concepts that I didn’t know after learning languages, frameworks, or even in lectures.&lt;/p&gt;

&lt;p&gt;These were concepts that I only came across when I wanted to use them in my project or when I faced some issues. The Multiselect Autocomplete Dropdown is one of the concepts that I didn’t learn when I was actually in the learning phase, but I learned it when I was building something on top of my learnings.&lt;/p&gt;





&lt;p&gt;In my project, I wanted a feature in the frontend where a user could send me multiple-selected values of one field, and I also wanted that field to be searchable and autocomplete.&lt;/p&gt;

&lt;p&gt;Then I noticed that this type of feature is also provided in HTML with the &lt;code&gt;select&lt;/code&gt; tag, but there was an exception with this tag, and the exception was that the tag is not searchable and does not support multiple values.&lt;/p&gt;

&lt;p&gt;So, I needed a different solution, and I wanted to figure out that solution.&lt;/p&gt;





&lt;p&gt;In the age of AI, finding a solution is very easy, but the difficult part was integrating it into my project.&lt;/p&gt;

&lt;p&gt;I found the solution with the help of AI, and now the next step is to integrate it into my project.&lt;/p&gt;





&lt;p&gt;I think this type of feature is learned only for a purpose. The topic is learned when the functionality is needed.&lt;/p&gt;

&lt;p&gt;When the topic is learned only for the purpose of learning, then the topic is not available in the course we want to learn from, documentation, or some video where the topic is not fully explained.&lt;/p&gt;

&lt;p&gt;In this case, I faced the same problem.&lt;/p&gt;





&lt;p&gt;If you face the same problem, please share your experience in the comments.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>react</category>
      <category>frontend</category>
    </item>
    <item>
      <title>Finally I access The Child Element</title>
      <dc:creator>Neel-Vekariya</dc:creator>
      <pubDate>Tue, 11 Aug 2026 04:21:58 +0000</pubDate>
      <link>https://dev.to/neel-vekariya/finally-i-access-the-child-element-29al</link>
      <guid>https://dev.to/neel-vekariya/finally-i-access-the-child-element-29al</guid>
      <description>&lt;p&gt;
Recently, if you read my little weird blogs, you know I learn &lt;b&gt;React.js&lt;/b&gt; and build one project.
In the project, I made many mistakes like creating a complex component structure.
Later I removed all components and made a clear folder structure.
&lt;/p&gt;

&lt;p&gt;
I created cards that did not show data, and I don’t know why.
And so on.
But one problem came when I used the React-Hook-Forms and my custom input field.
&lt;/p&gt;





&lt;p&gt;
This problem gave me more knowledge as well as an idea about how react-hook-form works.
The problem is like this.
When I want to access the child component from the parent component to access the data entered by the user.
&lt;/p&gt;

&lt;p&gt;
That gave me &lt;b&gt;undefined instead of the value&lt;/b&gt; which was passed by the user.
I was not able to find out why that happened.
Later I found out that my parent element does not have access to the input field.
That is the problem.
&lt;/p&gt;





&lt;p&gt;
Now I don’t know why the parent element does not access the child element.
In the parent element, I did not make a mistake in using the child element.
Then why did that happen.
&lt;/p&gt;

&lt;p&gt;
Subsequently, I found out that the &lt;b&gt;register&lt;/b&gt; which is used for accessing the value from the child element sends one ref to the child element.
But I did not define that or use that.
Now I found out the method that is used to pass reference to the child component.
&lt;/p&gt;





&lt;p&gt;
React provides a higher order function named &lt;b&gt;forwardRef&lt;/b&gt; to solve the problem of passing the reference to the child component.
The forwardRef is not needed in a normal input or select field.
&lt;/p&gt;

&lt;p&gt;
The custom component built for reusability blocks the reference.
In this situation, we need a service that passes the reference from the parent to the child component.
&lt;/p&gt;

&lt;p&gt;
The service is provided by the higher order function &lt;b&gt;forwardRef&lt;/b&gt;.
To use forwardRef in the custom component, you need to import it from React.
And pass it as a secondary separate parameter ref in the function.
&lt;/p&gt;

&lt;p&gt;
Now pass this parameter as a &lt;b&gt;ref property&lt;/b&gt; in the custom input component.
And wrap your exported component in &lt;b&gt;forwardRef&lt;/b&gt;.
Now you have access to the child component in the parent.
&lt;/p&gt;

&lt;p&gt;
And also have the value which is passed in your child component.
Now this function solved my problem of passing undefined value.
&lt;/p&gt;





&lt;p&gt;
If I have done something wrong, please let me know in the comments.
&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>frontend</category>
      <category>react</category>
      <category>javascript</category>
    </item>
    <item>
      <title>How I Prevent unauthorized user to access restricted routes</title>
      <dc:creator>Neel-Vekariya</dc:creator>
      <pubDate>Fri, 07 Aug 2026 04:55:29 +0000</pubDate>
      <link>https://dev.to/neel-vekariya/how-i-prevent-unauthorized-user-to-access-restricted-routes-50pj</link>
      <guid>https://dev.to/neel-vekariya/how-i-prevent-unauthorized-user-to-access-restricted-routes-50pj</guid>
      <description>&lt;p&gt;
In my last post, I talked about how I simplified managing all my components. 
After building all the components for my backend project, I assembled them into the home page and created a flow between them to navigate from one feature to another.
&lt;/p&gt;

&lt;p&gt;
But, I didn’t run the frontend for testing. 
Consequently, I did that, and obviously, I had many errors on the screen.
&lt;/p&gt;

&lt;p&gt;
Firstly, I solved all the errors I had and checked whether all the components were working properly or not. 
After checking all components, suddenly I found one security issue in my application.
&lt;/p&gt;

&lt;p&gt;
When I wanted to switch from one feature to another feature, I tried some kind of shortcut in the URL, and it worked. 
How, I don’t know.
&lt;/p&gt;

&lt;p&gt;
As a result, it rendered my component that was restricted for the user I logged in. 
This was a problem for me due to data security and the exposure of protected routes.
&lt;/p&gt;





&lt;p&gt;
I needed a solution. 
As a result, I searched about the problem and found out this is not only my problem but also a problem that all developers face.
&lt;/p&gt;

&lt;p&gt;
They have a centralized solution for that, in addition to using a &lt;b&gt;Protected component&lt;/b&gt;. 
The main idea of the protected component is to check given conditions.
&lt;/p&gt;

&lt;p&gt;
If the conditions are true, then render the protected component, else navigate them to the login or register page. 
The component is also used to boost user experience.
&lt;/p&gt;

&lt;p&gt;
Due to redirecting the user to the login or register page, this creates a smooth path for the user to use your application.
&lt;/p&gt;





&lt;p&gt;
Now let’s understand the working of the protected component. 
The component generally takes two parameters.
&lt;/p&gt;

&lt;p&gt;
The first one is children, which is the component we want to protect from unauthorized users. 
The second one is whether the user is logged in or not.
&lt;/p&gt;

&lt;p&gt;
Now in the component, we need access to the state that shows whether the user is logged in or not. 
Now we have two variables that decide whether the user is authenticated or not.
&lt;/p&gt;

&lt;p&gt;
Now one question may come to your mind: why do we give the second parameter when we already have access to the state that represents whether the user is logged in or not?
&lt;/p&gt;

&lt;p&gt;
The reason is that if you want optional control over the controller, as a result, you have special control over this component. 
Now we check the condition.
&lt;/p&gt;

&lt;p&gt;
For checking conditions, we use &lt;b&gt;useEffect&lt;/b&gt; to track all variable or state changes. 
Now if the condition is fulfilled, then render the protected component, else navigate.
&lt;/p&gt;





&lt;p&gt;
In the code section, I have done the same thing I mentioned.
&lt;/p&gt;



&lt;pre&gt;&lt;code&gt;
export default function Protected({ children, authenticated = true }) {
  const navigate = useNavigate();
  const authstatus = useSelector((state) =&amp;gt; state.auth.authstatus);

  useEffect(() =&amp;gt; {
    if (authenticated &amp;amp;&amp;amp; authstatus !== authenticated) {
      navigate("/login");
    } else if (!authenticated &amp;amp;&amp;amp; authstatus !== authenticated) {
      navigate("/home");
    }
  }, [authenticated, navigate, authstatus]);

  return children ? children : &amp;lt;h1&amp;gt;Loading...&amp;lt;/h1&amp;gt;;
}
&lt;/code&gt;
&lt;/pre&gt;



&lt;p&gt;
I am still learning frontend development, so if you find anything wrong or have suggestions, feel free to share your thoughts in the comments.
&lt;/p&gt;

&lt;p&gt;
Thanks for reading!
&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>react</category>
      <category>webdev</category>
      <category>frontend</category>
    </item>
    <item>
      <title>How I Simplified My React Project Using Feature-Based Component Structure</title>
      <dc:creator>Neel-Vekariya</dc:creator>
      <pubDate>Tue, 04 Aug 2026 04:44:50 +0000</pubDate>
      <link>https://dev.to/neel-vekariya/how-i-simplified-my-react-project-using-feature-based-component-structure-1dcp</link>
      <guid>https://dev.to/neel-vekariya/how-i-simplified-my-react-project-using-feature-based-component-structure-1dcp</guid>
      <description>&lt;p&gt;
Recently, I learned React.js, including how frontend development works, how to make the frontend responsive, and how to make reusable components. The only part that was missing to become a full-stack developer is now fulfilled.
&lt;/p&gt;

&lt;p&gt;
Now, I am officially a full-stack developer.
&lt;/p&gt;



&lt;p&gt;
In the journey of becoming a full-stack developer, I picked one project and improved it. First, I learned Node.js, Express.js, and MongoDB and developed one project using these technologies.
&lt;/p&gt;

&lt;p&gt;
The project I developed had many issues, so I built the same project again from scratch using the same technologies. At this point, I learned how to make a backend, but I wanted to learn Fastify and PostgreSQL, so I learned both of them.
&lt;/p&gt;

&lt;p&gt;
After learning Fastify and PostgreSQL, I built the same project using these technologies, and this helped me understand the backend deeply. Now, I have learned how to make backend APIs and databases.
&lt;/p&gt;

&lt;p&gt;
But one part was missing to develop a full product, and that is the frontend. I only knew how to make backend APIs and design databases.
&lt;/p&gt;



&lt;p&gt;
A month ago, I started learning how the frontend works, learned React.js, and other things like routers, hook-forms, localStorage, stores, etc. After learning the frontend, I made a React app for my backend project.
&lt;/p&gt;

&lt;p&gt;
But I didn’t know how to manage components, so I created components in a way that made my project complex to manage. I simply created one parent folder named "components", and in the parent folder, I created multiple subfolders like Button for the Button component, Login for the login page, Input for input fields, Register for the register page, Select for the dropdown component, Footer for the footer component, and so on.
&lt;/p&gt;

&lt;p&gt;
This means that I made one subfolder for each component, and now I realize this structure made it complex to manage all components.
&lt;/p&gt;





&lt;h2&gt;Why I Chose Feature-Wise Component Structure&lt;/h2&gt;

&lt;p&gt;
At this point, I decided to learn component structure next. I found many structures for managing components, but feature-wise component management is best for me.
&lt;/p&gt;

&lt;p&gt;
Why is it best for me? The answer is the backend project I built. In the project, I built role-wise login, logout, and features.
&lt;/p&gt;

&lt;p&gt;
Due to my backend project, I needed login, logout, register pages, and components for both roles. The feature-wise structure is best for me.
&lt;/p&gt;





&lt;h2&gt;How Feature-Wise Structure Works&lt;/h2&gt;

&lt;p&gt;
In a feature-wise structure, we build two main folders — the first one is "components" and the second one is "features". Now, we build role-wise subfolders in the "features" folder.
&lt;/p&gt;

&lt;p&gt;
In the "components" folder, we build components that are reusable for other components like Input, Button, and Select components. This is the main concept of the feature-wise component structure.
&lt;/p&gt;

&lt;p&gt;
The pages or components used for a specific feature that refers to a specific role are created in the role-wise folder. This structure removes complexity, which was a problem for me.
&lt;/p&gt;

&lt;p&gt;
Now, I add more components or features, and I manage all of this in a simple way. You get an idea from the reference image that I attached.
&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.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fxa83msfxfjal65r6j93h.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.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fxa83msfxfjal65r6j93h.png" alt="Feature-wise component structure" width="800" height="1000"&gt;&lt;/a&gt;&lt;br&gt;
&lt;br&gt;&lt;/p&gt;

&lt;p&gt;
I am still learning frontend development, so if you find anything wrong or have suggestions, feel free to share your thoughts in the comments.
&lt;/p&gt;

&lt;p&gt;
Thanks for reading!
&lt;/p&gt;

</description>
      <category>frontend</category>
      <category>webdev</category>
      <category>react</category>
      <category>fullstack</category>
    </item>
    <item>
      <title>How Local Storage improves the Frontend performance</title>
      <dc:creator>Neel-Vekariya</dc:creator>
      <pubDate>Sat, 01 Aug 2026 07:19:58 +0000</pubDate>
      <link>https://dev.to/neel-vekariya/how-local-storage-improves-the-frontend-performance-2h2i</link>
      <guid>https://dev.to/neel-vekariya/how-local-storage-improves-the-frontend-performance-2h2i</guid>
      <description>&lt;h1&gt;How Local Storage improves the performance&lt;/h1&gt;

&lt;p&gt;In the past many days, I learned about how frontend actually works and I learned lots of things. React is one of them. I learned how to manage state, how hooks work, how to make frontend faster and reliable, and the most important thing is how client-side data is actually stored using the local storage object.&lt;/p&gt;

&lt;p&gt;If you don’t know, the local storage object is provided by all web platforms by default.&lt;/p&gt;

&lt;h2&gt;How Local Storage Works&lt;/h2&gt;

&lt;p&gt;Now that we know where local storage actually lives, the next question is how local storage actually works. Let’s imagine you open one website, you change the theme, you select your preference, and you refresh the page.&lt;/p&gt;

&lt;p&gt;Now all your selected things or preferences get reset. And this is the problem. If your selected preferences or theme get reset, this creates a lack of user experience. To solve this problem, we actually use local storage.&lt;/p&gt;

&lt;p&gt;Now you know about which problem we face and how it can be solved by local storage. So the next question may come: how does it work?&lt;/p&gt;

&lt;p&gt;The working mechanism is when a user selects the preference or theme or any other things which boost user experience, then it is stored in client-side storage using local storage.&lt;/p&gt;

&lt;p&gt;The data is stored in object format. All browsers provide around 5MB of local storage for one domain. When you first time access the local storage data, it is read from the disk and loaded into the RAM, and the browser keeps that data in cache for faster access in the future. Now the second time you need data, we don’t carry data from the disk, we already have it in cache. That improves the user experience.&lt;/p&gt;

&lt;h2&gt;Implementation of Local Storage&lt;/h2&gt;

&lt;p&gt;Now you know how local storage actually works under the hood. The next question might come: how do we implement local storage?&lt;/p&gt;

&lt;p&gt;The answer is using the &lt;code&gt;setItem&lt;/code&gt; and &lt;code&gt;getItem&lt;/code&gt; methods of local storage. First we know about how to store created items or data in &lt;code&gt;localStorage&lt;/code&gt;, after that we talk about how the API data is stored.&lt;/p&gt;





&lt;h3&gt;1) Store Created Data in Local Storage&lt;/h3&gt;

&lt;p&gt;To store data into local storage, we need two types of data. First is created data or data which is extracted from the response, and second is already stored data in localStorage.&lt;/p&gt;

&lt;p&gt;Before combining, we need to convert stored data into an object. Now we combine both data and create one object. But we can’t store it directly into localStorage. To store data, first we need to convert that object into JSON format or string format. After converting into JSON or string format, we store it in localStorage using the setItem method.&lt;/p&gt;

&lt;p&gt;This is the code:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;
const res = await axios.post("https://example.com/users");   
const newData = response.data.data; 

const existingData = JSON.parse(localStorage.getItem("user")) || {};     // Get old data   

const mergedData = {     
  ...existingData,     
  ...newData   
};    // Merge    

localStorage.setItem("user", JSON.stringify(mergedData)); // Save again
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;In the code, “user” is the object where data is stored.&lt;/p&gt;

&lt;h3&gt;2) Store API Data with Deduplication&lt;/h3&gt;

&lt;p&gt;Now talk about the data which comes from the API. In this case, we again need two types of data. First is response data and second is the stored data which we need to convert into an object. After that, we combine both data, but we need to implement a filter for the data which is present in response and also in stored data, so we remove duplicate data. Now we merge into the object, convert into string, and store it into &lt;code&gt;localStorage&lt;/code&gt;. This is the code:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;
const localData = JSON.parse(localStorage.getItem("bookings")) || []; // Get old data 

const res = await axios.get("/api/shop/orders"); 
const apiData = res.data;  // New data

const merged = [ 
  ...localData, 
  ...apiData.filter((api) =&amp;gt; !localData.some((local) =&amp;gt; local.id === api.id)), // Filter the data which is already present
];  // Combine the data

localStorage.setItem('bookings', JSON.stringify(merged)); // Store the data
&lt;/code&gt;&lt;/pre&gt;





&lt;p&gt;I am still learning frontend development, so if you find anything wrong or have suggestions, feel free to share your thoughts in the comments.&lt;/p&gt;

&lt;p&gt;Thanks for reading!&lt;/p&gt;

</description>
      <category>frontend</category>
      <category>webdev</category>
      <category>react</category>
      <category>javascript</category>
    </item>
    <item>
      <title>How to Prevent Hanging Network Calls — What I Found After Investigating a Real Performance Issue</title>
      <dc:creator>Neel-Vekariya</dc:creator>
      <pubDate>Mon, 13 Jul 2026 03:26:53 +0000</pubDate>
      <link>https://dev.to/neel-vekariya/how-to-prevent-hanging-network-calls-what-i-found-after-investigating-a-real-performance-issue-1mfd</link>
      <guid>https://dev.to/neel-vekariya/how-to-prevent-hanging-network-calls-what-i-found-after-investigating-a-real-performance-issue-1mfd</guid>
      <description>&lt;p&gt;
In the past two months I explored a lot of things Express, Fastify, MongoDB, PostgreSQL, Node.js. I built around three to four projects in this time.
&lt;/p&gt;

&lt;p&gt;
All of them focused on backend because I haven't learned React yet. But the next one will be full stack.
&lt;/p&gt;

&lt;p&gt;
While building these projects, I noticed one issue that kept repeatedly coming back.
&lt;/p&gt;

&lt;p&gt;
Every time I called a database or an external service something that depends on a network call things would get weird.
&lt;/p&gt;

&lt;p&gt;
Sometimes the response never came. Sometimes it took way longer than it should. And this was creating a performance issue in my application that I couldn't ignore.
&lt;/p&gt;

&lt;p&gt;
So I investigated it properly. And I found that this is actually a common problem a lot of developers face this. And there's a standard solution for it.
&lt;/p&gt;





&lt;h2&gt;The Solution is a Timeout Wrapper&lt;/h2&gt;

&lt;p&gt;
The idea is simple. When you make a network call, instead of just calling it directly, you pass it through a timeout wrapper.
&lt;/p&gt;

&lt;p&gt;
If the call takes more time than it should, the wrapper skips or terminates the call and moves on.
&lt;/p&gt;

&lt;p&gt;
This keeps your application from getting stuck waiting on something that's never going to respond in time.
&lt;/p&gt;





&lt;h2&gt;Two Functions : Two Different Approaches In Timeout&lt;/h2&gt;

&lt;p&gt;
While building this, I found there are two ways to implement a timeout wrapper. Both solve the same problem but work very differently.
&lt;/p&gt;

&lt;h3&gt;1. withTimeout()&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;withTimeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;promise&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;ms&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;errorMessage&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Operation timed out&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;timeout&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;_&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="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;reject&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;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;errorMessage&lt;/span&gt;&lt;span class="p"&gt;)),&lt;/span&gt; &lt;span class="nx"&gt;ms&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="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;promise&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;timeout&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 function takes two things the promise (which is your network call) and the time limit in milliseconds.
&lt;/p&gt;

&lt;p&gt;
Inside, it creates a second promise called &lt;code&gt;timeout&lt;/code&gt;. This promise does one thing if the given time runs out, it rejects with an error message.
&lt;/p&gt;

&lt;p&gt;
Then it uses &lt;code&gt;Promise.race()&lt;/code&gt;. This function takes both promises and returns whichever one finishes first.
&lt;/p&gt;

&lt;p&gt;
If your network call responds in time, &lt;code&gt;Promise.race&lt;/code&gt; gives you that response. If the timeout fires first, you get the error.
&lt;/p&gt;

&lt;p&gt;
&lt;strong&gt;Simple. Clean. Works.&lt;/strong&gt;
&lt;/p&gt;

&lt;p&gt;
But there's a catch I found and this is the important part.
&lt;/p&gt;

&lt;p&gt;
&lt;code&gt;withTimeout()&lt;/code&gt; doesn't actually terminate the original network call. If the timeout fires and you move on, the network call is still running in the background.
&lt;/p&gt;

&lt;p&gt;
The socket is still open. Memory is still being used. CPU is still consumed. The request finishes eventually you just stopped waiting for it. The resources are still gone.
&lt;/p&gt;

&lt;p&gt;
This is where the second function solves a different problem.
&lt;/p&gt;



&lt;br&gt;
&lt;br&gt;

&lt;h3&gt;2. fetchWithTimeout()&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;fetchWithTimeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;options&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{},&lt;/span&gt; &lt;span class="nx"&gt;timeoutMs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="nx"&gt;_000&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;controller&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;AbortController&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;timer&lt;/span&gt; &lt;span class="o"&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="nx"&gt;controller&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;abort&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="nx"&gt;timeoutMs&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;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;url&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="nx"&gt;options&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;signal&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;controller&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;signal&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt;
        &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;finally&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;clearTimeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;timer&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 function takes the URL, options for the network call, and the timeout.
&lt;/p&gt;

&lt;p&gt;
The key difference here is &lt;code&gt;AbortController&lt;/code&gt;. This is a browser and Node.js API specifically built for canceling asynchronous operations without creating memory leaks.
&lt;/p&gt;

&lt;p&gt;
You create a controller, attach its signal to the fetch call, and start a timer. If the timer runs out before the call responds &lt;code&gt;controller.abort()&lt;/code&gt; is called.
&lt;/p&gt;

&lt;p&gt;
The fetch is terminated. Not just skipped actually terminated. All the resources that were being consumed get freed.
&lt;/p&gt;

&lt;p&gt;
And the &lt;code&gt;finally()&lt;/code&gt; at the end makes sure the timer gets cleared if the call does respond in time. No timer left hanging.
&lt;/p&gt;





&lt;h2&gt;The Real Difference Between Both&lt;/h2&gt;

&lt;p&gt;
This is the thing I want to make clear because it took me a bit to fully understand it.
&lt;/p&gt;

&lt;p&gt;
&lt;strong&gt;&lt;code&gt;withTimeout()&lt;/code&gt;&lt;/strong&gt; skips the wait. If the call takes too long, you move on. But the call is still running. Resources are still being consumed. You just stopped caring about the response.
&lt;/p&gt;

&lt;p&gt;
&lt;strong&gt;&lt;code&gt;fetchWithTimeout()&lt;/code&gt;&lt;/strong&gt; terminates the call. If it takes too long, the request is actually cancelled. Network socket freed. Memory freed. CPU freed.
&lt;/p&gt;

&lt;p&gt;
For most external API calls where you're using fetch, &lt;code&gt;fetchWithTimeout&lt;/code&gt; is the right choice because it actually cleans up after itself.
&lt;/p&gt;

&lt;p&gt;
For other types of async operations database queries, file reads, anything that returns a promise &lt;code&gt;withTimeout&lt;/code&gt; is what you'd use. Just know that the operation continues in the background even after the timeout fires.
&lt;/p&gt;





&lt;h2&gt;Why This Actually Matters&lt;/h2&gt;

&lt;p&gt;
When I was building my projects and network calls were just hanging my application was stuck. The user was waiting. The server was waiting. Nothing was happening.
&lt;/p&gt;

&lt;p&gt;
After adding timeout wrappers, if a call hangs, the application moves on. The user gets a proper error response instead of waiting forever. And with &lt;code&gt;fetchWithTimeout&lt;/code&gt;, the resources actually get freed instead of piling up in the background.
&lt;/p&gt;

&lt;p&gt;
This is the kind of thing that feels small but makes a real difference in how your application behaves under real conditions.
&lt;/p&gt;

&lt;p&gt;
&lt;strong&gt;One investigation. One problem. Two functions that solved it.&lt;/strong&gt;
&lt;/p&gt;







&lt;p&gt;
&lt;em&gt;If I got something wrong or anything can be improved please drop it in the comments. I'm still learning and I want to get this right.&lt;/em&gt;
&lt;/p&gt;

</description>
      <category>node</category>
      <category>javascript</category>
      <category>webdev</category>
      <category>programming</category>
    </item>
    <item>
      <title>The Next Token, Not the Next Script</title>
      <dc:creator>Neel-Vekariya</dc:creator>
      <pubDate>Wed, 08 Jul 2026 05:45:51 +0000</pubDate>
      <link>https://dev.to/neel-vekariya/the-next-token-not-the-next-script-5cgm</link>
      <guid>https://dev.to/neel-vekariya/the-next-token-not-the-next-script-5cgm</guid>
      <description>&lt;p&gt;Everyone is using AI to write code now. But I think the real future of programming isn't AI generating full scripts or full products it's AI predicting the next token, and us deciding whether that token is right or not.&lt;/p&gt;

&lt;p&gt;Here's why. When you write code manually, you're already doing something similar in your head predicting the next line based on patterns you've seen before, then checking if it's correct as you write it. Every line passes through your judgment.&lt;/p&gt;

&lt;p&gt;When AI generates a full script or product, that judgment gets skipped. You review the code, sure, but not the way you'd review your own. You skim  "this line does X, this line does Y" and move on. You're checking outcomes, not making decisions. That's a completely different mental process, and it's shallower.&lt;/p&gt;

&lt;p&gt;But when AI suggests the next token, or the next line, instead of the next thousand lines, you're forced back into the same loop you'd use writing manually: is this right, or not? You stay the one making the call.&lt;/p&gt;

&lt;p&gt;That's why I think the best use of AI in programming isn't generate my whole app. It's predict what comes next, and let me decide. Smaller suggestions keep engineers in the driver's seat. Full generations quietly take the wheel.&lt;/p&gt;

</description>
      <category>ai</category>
      <category>programming</category>
      <category>discuss</category>
      <category>coding</category>
    </item>
    <item>
      <title>Request Scoped Context – How I Stopped Passing Arguments Everywhere</title>
      <dc:creator>Neel-Vekariya</dc:creator>
      <pubDate>Mon, 29 Jun 2026 07:40:02 +0000</pubDate>
      <link>https://dev.to/neel-vekariya/request-scoped-context-how-i-stopped-passing-arguments-everywhere-gm</link>
      <guid>https://dev.to/neel-vekariya/request-scoped-context-how-i-stopped-passing-arguments-everywhere-gm</guid>
      <description>&lt;p&gt;In my past two backend projects, I use to pass info about user manually. I thought this is the right method. Every time some function need info, I pass it as parameter. Simple. Direct. Clean enough.&lt;/p&gt;

&lt;p&gt;Then I started building a cloth rent booking app.&lt;/p&gt;

&lt;p&gt;In start everything was fine. I had &lt;code&gt;orderId&lt;/code&gt; coming in with the request. I pass it to my booking service. Service pass it to availability checker. I thought — this is clean approach. I feel good about the code.&lt;br&gt;
&lt;br&gt;&lt;br&gt;
Then the app start getting more complex.&lt;/p&gt;

&lt;p&gt;Now my &lt;code&gt;orderId&lt;/code&gt; need to travel from controller to booking service to availability checker to pricing calculator to logger. Every function in this chain need that &lt;code&gt;orderId&lt;/code&gt; as parameter. &lt;/p&gt;

&lt;p&gt;Even functions which are not using &lt;code&gt;orderId&lt;/code&gt; directly they just holding it for a moment and passing it to next function. Function is doing nothing with this parameter except passing it forward. &lt;/p&gt;

&lt;p&gt;Like a relay race where some runners are not actually running. They just standing there, holding the baton, waiting to give it to someone else.&lt;/p&gt;

&lt;p&gt;This is what called &lt;strong&gt;parameter pollution&lt;/strong&gt;. I didn't know the name then. I just knew something is wrong.&lt;/p&gt;

&lt;p&gt;The second problem hit me when I need to add new info deep in the execution. That mean I go back and add that parameter to every function in the chain. &lt;/p&gt;

&lt;p&gt;Not because all function use it just because they are sitting in the middle. One change, five files touched. This is &lt;strong&gt;tight coupling&lt;/strong&gt;. Changing something simple create maintenance overhead everywhere.&lt;/p&gt;

&lt;p&gt;Third problem is logging. To attach &lt;code&gt;orderId&lt;/code&gt; to every log entry I need to carry it all the way down the entire execution path. More parameters. More same work again and again. Copy paste same argument in every function signature.&lt;/p&gt;

&lt;p&gt;I was writing same thing again and again. This kind of repetition is not productive. This is just noise.&lt;/p&gt;



&lt;p&gt;So I start searching for better approach. How to pass context without manually threading it through every call. And I found something I never heard before.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;AsyncLocalStorage.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;My first reaction was wow. This is exact solution for my problem.&lt;/p&gt;

&lt;p&gt;AsyncLocalStorage is a Node.js feature from the &lt;code&gt;async_hooks&lt;/code&gt; module. The idea is simple. You create a storage that is scoped to a specific async execution meaning one request get its own private storage. &lt;/p&gt;

&lt;p&gt;You store your &lt;code&gt;orderId&lt;/code&gt;, &lt;code&gt;userId&lt;/code&gt;, or any context once at the entry point of the request. After that, any function in the entire execution chain can read it directly. No passing. No threading. No pollution.&lt;/p&gt;

&lt;p&gt;Node.js track async context internally. When you start a new context using AsyncLocalStorage, every async function running inside it inherit access to that same storage. No matter how deep the call stack goes. The info is just there, available, without anyone carrying it.&lt;/p&gt;

&lt;p&gt;To build this in your project you need two functions only.&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;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;AsyncLocalStorage&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;async_hooks&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;requestContext&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;AsyncLocalStorage&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;runWithContext&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;context&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;fn&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="nx"&gt;requestContext&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;context&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;fn&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="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;getContext&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="nx"&gt;requestContext&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getStore&lt;/span&gt;&lt;span class="p"&gt;()&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;runWithContext&lt;/code&gt; is called at the entrance of your request in your middleware or route handler. You pass the context object with whatever info you need, and the function to run. Everything inside that function now have access to the same storage automatically.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;getContext&lt;/code&gt; is called anywhere inside the execution deep inside a service, inside a logger, inside a helper. It reads from the current async context. No parameters needed. No importing shared state. Just call it and get what you need.&lt;/p&gt;




&lt;p&gt;Now my logger not need &lt;code&gt;orderId&lt;/code&gt; as a parameter. Just call &lt;code&gt;getContext()&lt;/code&gt; and read it. My availability checker not carrying a parameter it don't use. My pricing calculator is clean. Every function only take what it actually need to do its job.&lt;/p&gt;

&lt;p&gt;The code became honest. Functions stop pretending to care about things they don't care about.&lt;/p&gt;

&lt;p&gt;When I first saw this working in my booking app the feeling was like finally. Not just because code was cleaner. But because I understand why the old approach was a problem in first place. &lt;/p&gt;

&lt;p&gt;Small codebase hide these problems. You don't feel the pain until complexity grows. By then bad pattern is already everywhere and refactoring is expensive.&lt;/p&gt;

&lt;p&gt;This is one thing I wish I know earlier. Not because it is hard to understand it is actually simple. But because the problem it solve only become visible at certain scale. And when you finally see it, you can't unsee it.&lt;/p&gt;

&lt;p&gt;If you are building something today and passing same parameter through three or more functions just to reach one inner function this is your solution. &lt;/p&gt;

&lt;p&gt;Store it once at the entry point. Read it anywhere. Let your functions stay focused on what they actually do.&lt;br&gt;
&lt;/p&gt;

&lt;br&gt;&lt;br&gt;
&lt;strong&gt;If I made any mistakes in this — please mention in the comments. I'll correct it.&lt;/strong&gt;

</description>
      <category>node</category>
      <category>javascript</category>
      <category>backend</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Why Problems Keep Me Going in Tech</title>
      <dc:creator>Neel-Vekariya</dc:creator>
      <pubDate>Thu, 25 Jun 2026 07:40:29 +0000</pubDate>
      <link>https://dev.to/neel-vekariya/why-problems-keep-me-going-in-tech-794</link>
      <guid>https://dev.to/neel-vekariya/why-problems-keep-me-going-in-tech-794</guid>
      <description>&lt;p&gt;As a learner in tech, Every day somehow I face a new problem I didn't even know existed yesterday.&lt;/p&gt;

&lt;p&gt;And honestly I love this.&lt;/p&gt;

&lt;p&gt;When something crashes, when a feature breaks, when I have no idea why the code is doing what it's doing and that struggle of finding the bug, digging into it, understanding it that gives me a kinda of motivation I can't explain. It's like a dopamine hit. The feeling after solving that problem is incredible. &lt;/p&gt;

&lt;p&gt;&lt;br&gt;&lt;br&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.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F4i6zq1xfs3pl317h6g8q.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.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F4i6zq1xfs3pl317h6g8q.gif" alt=" " width="320" height="566"&gt;&lt;/a&gt;&lt;br&gt;
&lt;br&gt;&lt;/p&gt;

&lt;p&gt;I realized something because of this. Without problems, writing code becomes a repetitive, boring task. Zero motivation. Zero excitement. Just the same thing again and again.&lt;/p&gt;

&lt;p&gt;But when something breaks when I actually have to think, investigate, sit with the problem that's when my boundaries expand. That's when I actually learn something.&lt;br&gt;
&lt;br&gt;&lt;br&gt;
&lt;strong&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;h3&gt;&lt;strong&gt;The Comfort Zone&lt;/strong&gt;&lt;/h3&gt;


&lt;p&gt;But I also noticed something else about myself.&lt;/p&gt;

&lt;p&gt;The moment I get comfortable with something I lose interest in it.&lt;/p&gt;

&lt;p&gt;It starts in the learning phase. Everything is new, everything is a problem, everything is exciting.&lt;/p&gt;

&lt;p&gt;I'm struggling, I'm figuring things out, I'm building. And then slowly I get comfortable. That struggle disappears.&lt;/p&gt;

&lt;p&gt;The thing that was exciting becomes a repetitive task. Boring. Routine.&lt;/p&gt;

&lt;p&gt;And then I move on to the next thing.&lt;/p&gt;

&lt;p&gt;This happened with MongoDB. I started learning it, faced problems connection issues, queries not giving correct responses, things not working the way I expected.&lt;/p&gt;

&lt;p&gt;I struggled. I used it in my backend project. And then I got comfortable. The interest just went away. MongoDB became something I already knew.&lt;/p&gt;

&lt;p&gt;So I moved to PostgreSQL.&lt;/p&gt;

&lt;p&gt;Same thing happened with Express. I got comfortable, lost interest. &lt;/p&gt;

&lt;p&gt;That's actually one of the reasons I moved to Fastify I needed something new to struggle with again.&lt;/p&gt;



&lt;br&gt;&lt;br&gt;
&lt;strong&gt;&lt;h3&gt;The Interesting Part&lt;/h3&gt;&lt;/strong&gt;&lt;br&gt;
At first I thought this was a problem. Like maybe I'm not focusing enough. Maybe I should stay with one thing longer.

&lt;p&gt;But then I noticed something.&lt;/p&gt;

&lt;p&gt;Express took me two and a half months to learn. Fastify took one week. &lt;/p&gt;

&lt;p&gt;PostgreSQL took even less time than that.&lt;/p&gt;

&lt;p&gt;The time it takes me to learn something new keeps getting smaller. Each thing I learn becomes the foundation for the next thing. The struggles I had before make the new struggles easier to work through.&lt;/p&gt;

&lt;p&gt;Someone once said "Tomorrow's knowledge becomes the foundation of today."&lt;/p&gt;

&lt;p&gt;I think about this a lot, And Thats True. Every uncomfortable thing I pushed through, every bug that frustrated me, every late night trying to figure out why something wasn't working all of that is why the next hard thing feels a little less hard.&lt;br&gt;
&lt;/p&gt;

&lt;br&gt;&lt;br&gt;
&lt;strong&gt;&lt;h3&gt;What I Actually Think This Is&lt;/h3&gt;&lt;/strong&gt;&lt;br&gt;
I don't think I get bored of things. I think I outgrow them.&lt;br&gt;
The problems that once challenged me stop being problems. And without problems, there's no growth. So naturally I move toward the next thing that will challenge me again.

&lt;p&gt;That feeling the discomfort, the struggle, the not knowing that's not a sign something is wrong. That's actually the sign that learning is happening.&lt;/p&gt;

&lt;p&gt;I want to stay in that feeling as long as I can.&lt;br&gt;
&lt;/p&gt;

&lt;br&gt;&lt;br&gt;
If I made any mistakes in this — please mention in the comments. I'll correct it.

</description>
      <category>learning</category>
      <category>programming</category>
      <category>career</category>
      <category>discuss</category>
    </item>
    <item>
      <title>Circuit Breaker Explained Through Real Failure Experience</title>
      <dc:creator>Neel-Vekariya</dc:creator>
      <pubDate>Mon, 22 Jun 2026 06:33:06 +0000</pubDate>
      <link>https://dev.to/neel-vekariya/circuit-breaker-explained-through-real-failure-experience-3aeg</link>
      <guid>https://dev.to/neel-vekariya/circuit-breaker-explained-through-real-failure-experience-3aeg</guid>
      <description>&lt;p&gt;In the past few days I faced something I didn't expect.&lt;/p&gt;

&lt;p&gt;
Features breaking one after another. Endpoints not working. Failures coming in a chain one failing, that causing another to fail, and that causing another. And I had no control over any of it. No way to stop it. No way to understand why it was happening. The failures just kept coming and my system kept getting more overloaded.
&lt;/p&gt;

&lt;p&gt;
I won't lie I wanted to quit. Multiple times. I hit a point where I thought maybe this is not for me.
&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.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fv695l6gja4ugw4cfkvqi.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.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fv695l6gja4ugw4cfkvqi.gif" alt="Developer overwhelmed by continuous system failures and errors" width="220" height="112"&gt;&lt;/a&gt;
&lt;/p&gt;

&lt;p&gt;
But there's something that keeps pulling me back. Curiosity. That one feeling of I want to understand this. I want to fix this. That's the fuel that kept me going even when I was completely frustrated.
&lt;/p&gt;

&lt;p&gt;
And I gave it another chance. I sat with the problem again. And this time I found something that explained exactly what was happening and gave me a way to fix it.
&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;That thing is called a circuit breaker.&lt;/strong&gt;&lt;/p&gt;




&lt;h2&gt;What Was Actually Happening&lt;/h2&gt;

&lt;p&gt;
Before I found this solution, my system was doing something really inefficient and I didn't even realize it.
&lt;/p&gt;

&lt;p&gt;
Every time a service was failing, I was still sending requests to it. The service is down, failing, completely broken and I'm still passing requests through the router to it. Every single one of them failing. My server kept trying, resources kept getting consumed, and nothing was recovering. The failures were just piling up.
&lt;/p&gt;

&lt;p&gt;
What I needed was a way to say okay, this service has failed too many times. Stop sending requests to it. Give it time to recover. Then try again carefully.
&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;That's exactly what a circuit breaker does.&lt;/strong&gt;&lt;/p&gt;




&lt;h2&gt;The Electrical Analogy&lt;/h2&gt;

&lt;p&gt;
Think about an electrical circuit breaker in your home. When too much current flows something is overloaded, something is wrong the breaker trips. It opens the circuit. Power stops flowing. This protects everything from burning out.
&lt;/p&gt;

&lt;p&gt;
Then after a while, you reset it carefully. If everything is fine, the circuit closes again and power flows normally.
&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Code circuit breakers work exactly the same way.&lt;/strong&gt;&lt;/p&gt;




&lt;h2&gt;Three States: This is the Core Idea&lt;/h2&gt;

&lt;h3&gt;CLOSED&lt;/h3&gt;

&lt;p&gt;Normal operation. All requests are allowed through. But failures are being tracked. If the number of failures crosses the threshold you set, the circuit opens.&lt;/p&gt;

&lt;h3&gt;OPEN&lt;/h3&gt;

&lt;p&gt;
The circuit has tripped. All requests are blocked immediately. No function even runs. The system waits for the cooldown period to pass. This is the healing time your server recovers, the failing service gets time to come back up.
&lt;/p&gt;

&lt;h3&gt;HALF_OPEN&lt;/h3&gt;

&lt;p&gt;
After the cooldown, the circuit doesn't immediately go back to CLOSED. It moves to HALF_OPEN first. A limited number of requests are allowed through just enough to test whether the service has actually recovered. If those requests succeed, the circuit closes again and everything goes back to normal. If they fail, the circuit opens again and the cooldown starts over.
&lt;/p&gt;




&lt;h2&gt;The Code&lt;/h2&gt;

&lt;pre&gt;&lt;code&gt;
export class CircuitBreaker {
    constructor(failureThreshold, cooldownMs) {
        this.failureThreshold = failureThreshold
        this.cooldownMs = cooldownMs
        this.state = "CLOSED"
        this.failureCount = 0
        this.lastFailureTime = null
    }

    openCircuit() {
        this.state = "OPEN"
        this.lastFailureTime = Date.now()
    }

    closeCircuit() {
        this.state = "CLOSED"
        this.failureCount = 0
        this.lastFailureTime = null
    }

    halfOpenCircuit() {
        this.state = "HALF_OPEN"
    }

    async execute(fn) {
        if (this.state === "OPEN") {
            const cooldownExpired = Date.now() - this.lastFailureTime &amp;gt;= this.cooldownMs
            if (!cooldownExpired) {
                throw new ApiError(503, "Circuit is open.")
            }
            this.halfOpenCircuit();
        }

        try {
            const result = await fn()
            if (this.state === "HALF_OPEN") {
                this.closeCircuit()
            }
            return result;
        } catch (error) {
            if (this.state === "HALF_OPEN") {
                this.openCircuit()
                throw error;
            }
            this.failureCount++
            if (this.failureCount &amp;gt;= this.failureThreshold) {
                this.openCircuit()
            }
            throw error
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;




&lt;h2&gt;Where This Actually Gets Used&lt;/h2&gt;

&lt;p&gt;
This isn't just for one specific case. Any time your system depends on an external service that can fail, a circuit breaker makes sense.
&lt;/p&gt;

&lt;p&gt;
Payment gateways. External APIs. Microservices talking to each other. Database connections. Third party integrations. All of these can fail temporarily. And without a circuit breaker, your system will keep hammering them even when they're down wasting resources, slowing everything else down, and making recovery harder.
&lt;/p&gt;




&lt;h2&gt;What This Actually Fixed for Me&lt;/h2&gt;

&lt;p&gt;
Before circuit breaker failures came in a chain. One service went down, requests kept hitting it, my system kept consuming resources on requests that were definitely going to fail, and everything got worse.
&lt;/p&gt;

&lt;p&gt;
After circuit breaker the moment failures cross the threshold, the circuit opens. Requests stop hitting the failing service. The system gets breathing room. The service gets time to recover. And when it comes back, HALF_OPEN tests it carefully before trusting it again.
&lt;/p&gt;

&lt;p&gt;
That frustration I had the failures I couldn't control, the system I couldn't stabilize all of it was because I had no mechanism to stop the bleeding when something went wrong.
&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Circuit breaker is that mechanism.&lt;/strong&gt;&lt;/p&gt;




&lt;p&gt;If I got something wrong or anything can be improved — please drop it in the comments. I'm still learning and I want to get this right.&lt;/p&gt;

</description>
      <category>backend</category>
      <category>systemdesign</category>
      <category>javascript</category>
      <category>devops</category>
    </item>
    <item>
      <title>Semaphore — How to Control How Many Operations Run at the Same Time</title>
      <dc:creator>Neel-Vekariya</dc:creator>
      <pubDate>Wed, 17 Jun 2026 06:45:03 +0000</pubDate>
      <link>https://dev.to/neel-vekariya/semaphore-how-to-control-how-many-operations-run-at-the-same-time-4l5a</link>
      <guid>https://dev.to/neel-vekariya/semaphore-how-to-control-how-many-operations-run-at-the-same-time-4l5a</guid>
      <description>&lt;p&gt;What if 100 requests hit your server at the same time and all of them try to connect to your database simultaneously?&lt;/p&gt;

&lt;p&gt;I started thinking about this while building my project. I had worker threads running, multiple operations happening at once, and at some point I realized there's no control here. Everything is just running at the same time. No limit. No waiting. Just all of it, at once.&lt;br&gt;
And that's a problem.&lt;br&gt;
&lt;/p&gt;

&lt;br&gt;&lt;br&gt;
&lt;strong&gt;&lt;h3&gt;What the actual means&lt;/h3&gt;&lt;/strong&gt;&lt;br&gt;
Think about a restaurant with only 10 tables.&lt;br&gt;
100 people want to eat. But only 10 can sit at a time. The rest wait outside. When one table frees up, the next person in line comes in. Nobody rushes in randomly. There's order. There's control.

&lt;p&gt;That's exactly what a semaphore does for your code.&lt;/p&gt;

&lt;p&gt;A semaphore controls how many operations can run at the same time. If the limit is reached, the rest wait in a queue. When one operation finishes and releases its slot, the next one in the queue gets to run.&lt;br&gt;
&lt;br&gt;&lt;/p&gt;



&lt;h3&gt;Why This Actually Matters&lt;/h3&gt;

&lt;p&gt;Without this kind of control, you can easily overwhelm a resource.&lt;/p&gt;

&lt;p&gt;Your database has a connection limit. Your external API has a rate limit. &lt;/p&gt;

&lt;p&gt;Your file system slows down if too many things try to write at the same time. If you just let everything run at once without any control, you're not managing your resources you're just hoping nothing breaks.&lt;/p&gt;

&lt;p&gt;I kept seeing this pattern while building production-level infrastructure. Every piece of it has some form of concurrency control. Rate limiting for incoming requests. Connection pooling for databases. And semaphore for controlling how many operations run at a time inside your own system.&lt;br&gt;
&lt;/p&gt;

&lt;br&gt;&lt;br&gt;
&lt;strong&gt;&lt;h3&gt;How the Semaphore Works&lt;/h3&gt;&lt;/strong&gt;

&lt;p&gt;The implementation is simpler than it sounds.&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;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Semaphore&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;maxConcurrent&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;maxConcurrent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;maxConcurrent&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;current&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;queue&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="nf"&gt;acquire&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="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;current&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;maxConcurrent&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;current&lt;/span&gt;&lt;span class="o"&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="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;queue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&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="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;release&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;current&lt;/span&gt;&lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;queue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;current&lt;/span&gt;&lt;span class="o"&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;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;queue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;shift&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
            &lt;span class="nf"&gt;next&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;async&lt;/span&gt; &lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;fn&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;acquire&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="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;fn&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;finally&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;release&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;Three parts. That's it.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;acquire()&lt;/code&gt; — before an operation starts, it asks the semaphore for a slot. If a slot is available, it gets one and runs immediately. If not, it waits in the queue. That waiting is handled by a Promise that only resolves when a slot opens up.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;release()&lt;/code&gt; — when an operation finishes, it gives the slot back. And if there's anything waiting in the queue, the next one gets the slot immediately.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;run()&lt;/code&gt; — this is the wrapper you actually use. It acquires the slot, runs your function, and releases in a finally block. The finally is important it makes sure the slot is always released, even if the operation throws an error. No slot gets stuck.&lt;/p&gt;



&lt;br&gt;

&lt;h3&gt;What This Looks Like in Practice&lt;/h3&gt;

&lt;p&gt;Say you have 50 API calls to make but you only want 5 running at the same time.&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;semaphore&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;Semaphore&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&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;results&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&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;requests&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;semaphore&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;run&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;callAPI&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="p"&gt;);&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;All 50 are queued up. But only 5 run at a time. As each one finishes, the next one in line starts. Controlled. Predictable. Your API doesn't get overwhelmed.&lt;br&gt;
&lt;/p&gt;

&lt;br&gt;

&lt;h3&gt;What I Realized&lt;/h3&gt;

&lt;p&gt;When I first wrote concurrent code, I just used &lt;code&gt;Promise.all()&lt;/code&gt;and let everything run at the same time. It worked. Until it didn't.&lt;/p&gt;

&lt;p&gt;The moment the load increased too many database connections, too many simultaneous operations,things started breaking. And I didn't understand why at first because the code looked fine.&lt;/p&gt;

&lt;p&gt;That's the thing about concurrency. It's not just about making things run at the same time. It's about controlling how many things run at the same time. That control is what keeps your system stable under real load.&lt;br&gt;
Semaphore is one of the simplest ways to add that control. And once you understand it, you start seeing places where you need it everywhere.&lt;br&gt;
&lt;/p&gt;

&lt;br&gt;&lt;br&gt;
If I got something wrong or anything can be improved — please drop it in the comments. I'm still learning and I want to get this right.

</description>
      <category>architecture</category>
      <category>learning</category>
      <category>performance</category>
      <category>writing</category>
    </item>
  </channel>
</rss>
