<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Shravanidalvi on Medium]]></title>
        <description><![CDATA[Stories by Shravanidalvi on Medium]]></description>
        <link>https://medium.com/@shravanidalvi22?source=rss-34efbe7f857e------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*UCmMAyJiYSEAI3Mk</url>
            <title>Stories by Shravanidalvi on Medium</title>
            <link>https://medium.com/@shravanidalvi22?source=rss-34efbe7f857e------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 23:22:22 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@shravanidalvi22/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[JavaScript Is Single-Threaded… So How Does It Feel So Fast?]]></title>
            <link>https://medium.com/@shravanidalvi22/javascript-is-single-threaded-so-how-does-it-feel-so-fast-502aa5c7e0a7?source=rss-34efbe7f857e------2</link>
            <guid isPermaLink="false">https://medium.com/p/502aa5c7e0a7</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[promises]]></category>
            <category><![CDATA[callback-hell]]></category>
            <category><![CDATA[promises-in-javascript]]></category>
            <category><![CDATA[asynchronous-programming]]></category>
            <dc:creator><![CDATA[Shravanidalvi]]></dc:creator>
            <pubDate>Thu, 05 Mar 2026 11:33:03 GMT</pubDate>
            <atom:updated>2026-03-05T11:33:03.468Z</atom:updated>
            <content:encoded><![CDATA[<p>If JavaScript runs on just <strong>one thread</strong>, how does it power platforms like Netflix, Instagram, and Uber?</p><p>Here’s the twist:</p><p>JavaScript is <strong>synchronous by nature</strong>.<br>But it behaves <strong>asynchronous by design</strong>.</p><p>Let’s truly understand what that means — without confusion.</p><h3>Part 1 — JavaScript Is Synchronous (And That’s Good)</h3><p>At its core, JavaScript:</p><ul><li>Runs on a <strong>single thread</strong></li><li>Has <strong>one Call Stack</strong></li><li>Executes <strong>one task at a time</strong></li><li>Follows <strong>top-to-bottom execution</strong></li></ul><p>Example:</p><pre>console.log(&quot;Start&quot;);<br>console.log(&quot;Middle&quot;);<br>console.log(&quot;End&quot;);</pre><p>Output:</p><pre>Start<br>Middle<br>End</pre><p>Each line waits for the previous line to finish.</p><p>That’s synchronous execution.</p><h3>Why Single-Threaded?</h3><p>JavaScript was created in 1995 for browsers like Netscape Navigator.</p><p>Its main job?<br>Manipulate the DOM safely.</p><p>If two threads tried changing the same button at the same time:</p><ul><li>One changes color</li><li>One deletes it</li><li>One updates text</li></ul><p>Result? Chaos.</p><p>Single-threading makes behavior predictable and safe.</p><h3>The Real Problem: Blocking Code</h3><p>Consider this:</p><pre>console.log(&quot;Start&quot;);</pre><pre>for (let i = 0; i &lt; 10000000000; i++) {}</pre><pre>console.log(&quot;End&quot;);</pre><p>The browser freezes.</p><p>Why?</p><p>Because JavaScript must finish the loop before moving forward.</p><p>Now imagine this happening when:</p><ul><li>Fetching API data</li><li>Uploading a file</li><li>Reading a database</li><li>Waiting 3 seconds</li></ul><p>If JavaScript blocked during these tasks…</p><p>Your app would feel broken.</p><p>So how does it avoid blocking?</p><h3>Part 2 — The Event Loop: The Real Hero</h3><p>JavaScript alone is synchronous.</p><p>But when running inside:</p><ul><li>A browser like Google Chrome</li><li>Or a runtime like Node.js</li></ul><p>It gains access to background systems.</p><p>This system includes:</p><ul><li>Web APIs</li><li>Task Queue</li><li>Microtask Queue</li><li>Event Loop</li></ul><h3>Simple Mental Model</h3><p>Imagine:</p><p>You are a manager (Call Stack).<br>You can only do one task at a time.</p><p>When a long task comes:</p><p>You delegate it to a worker (Web API).</p><p>When the worker finishes:</p><p>They stand in a queue.</p><p>When you’re free:</p><p>You pick the next task from the queue.</p><p>That’s the Event Loop.</p><p>Now let’s understand how developers actually write async code.</p><p>This is where callbacks, promises, and async/await come in.</p><h3>Part 3 — Callbacks (The Foundation)</h3><blockquote><em>A callback is function passed as an argument to another function, executed later.</em></blockquote><p>Example:</p><pre>setTimeout(() =&gt; {<br>  console.log(&quot;Executed after 2 seconds&quot;);<br>}, 2000);</pre><h3>What’s Happening Internally?</h3><ol><li>setTimeout goes to Web API.</li><li>Timer runs in background.</li><li>After 2 seconds → callback goes to Task Queue.</li><li>Event Loop pushes it to Call Stack when empty.</li></ol><p>JavaScript never waited.</p><h3>Real-Life Example</h3><p>You order food at a restaurant.</p><p>Instead of standing at the kitchen waiting…</p><p>You give your number.</p><p>When food is ready, they call you.</p><p>That phone call is the callback.</p><h3>The Problem: Callback Hell</h3><p>Example:</p><pre>getUser(function(user) {<br>  getOrders(user, function(orders) {<br>    getPayment(orders, function(payment) {<br>      console.log(payment);<br>    });<br>  });<br>});</pre><p>Problems:</p><ul><li>Hard to read</li><li>Deep nesting</li><li>Error handling becomes messy</li><li>Difficult to maintain</li></ul><p>This is called <strong>Callback Hell</strong> or the <strong>Pyramid of Doom</strong>.</p><p>That’s why Promises were introduced.</p><h3>Part 4 — Promises (Structured Async)</h3><blockquote><em>A promise is an object that represents the eventual completion(or failure) of an asynchronous operation and its resulting value.</em></blockquote><p>It has 3 states:</p><ul><li>Pending</li><li>Fulfilled</li><li>Rejected</li></ul><h3>Basic Promise Example</h3><pre>const promise = new Promise((resolve, reject) =&gt; {<br>  setTimeout(() =&gt; {<br>    resolve(&quot;Data received&quot;);<br>  }, 2000);<br>});</pre><pre>promise.then(result =&gt; {<br>  console.log(result);<br>});</pre><p>Here:</p><ul><li>resolve() → success</li><li>reject() → failure</li><li>.then() → runs when resolved</li><li>.catch() → handles errors</li></ul><h3>Real-Life Example</h3><p>Ordering something online:</p><ol><li>Order placed → Pending</li><li>Delivered → Fulfilled</li><li>Cancelled → Rejected</li></ol><p>You don’t block your life waiting for delivery.</p><p>You continue doing other things.</p><p>When delivery happens → you respond.</p><p>That’s a Promise.</p><h3>Promise Chaining</h3><pre>fetch(&quot;https://api.example.com&quot;)<br>  .then(response =&gt; response.json())<br>  .then(data =&gt; console.log(data))<br>  .catch(error =&gt; console.log(error));</pre><p>Advantages:</p><ul><li>No deep nesting</li><li>Clear structure</li><li>Centralized error handling</li></ul><p>But chaining multiple .then() calls can still get messy.</p><p>That’s why async/await was created.</p><h3>Part 5 — Async/Await (Synchronous Style, Async Power)</h3><p>Introduced in ES2017.</p><p>Built on top of Promises.</p><p>It makes async code look synchronous.</p><h3>Basic Example</h3><pre>async function getData() {<br>  try {<br>    const response = await fetch(&quot;https://api.example.com&quot;);<br>    const data = await response.json();<br>    console.log(data);<br>  } catch (error) {<br>    console.log(error);<br>  }<br>}</pre><h3>What Does async Do?</h3><p>It makes a function return a Promise automatically.</p><h3>What Does await Do?</h3><p>It pauses execution of the function <strong>without blocking the thread</strong>.</p><p>Important:</p><p>It only pauses inside that async function — <br>not the entire JavaScript engine.</p><h3>Real-Life Example</h3><p>Imagine you’re cooking pasta.</p><p>Instead of:</p><p>Standing and staring at boiling water…</p><p>You:</p><ul><li>Put pasta in water</li><li>Set timer</li><li>Chop vegetables</li><li>Do other tasks</li></ul><p>When timer rings → you continue.</p><p>That’s await.</p><p>It pauses the function’s flow<br>but does not freeze the world.</p><h3>Example</h3><h4>1. The Call Stack</h4><p>The <strong>Call Stack</strong> is where the main thread of execution lives. JavaScript is single-threaded, meaning it can only do one thing at a time.</p><ul><li>When a function is called, it is pushed onto the stack.</li><li>The <strong>Global Execution Context</strong> sits at the bottom of the stack.</li><li>In the diagram, you see AsyncOp1() and Promise.then() being processed; once a function finishes, it is &quot;popped&quot; off the stack.</li></ul><h4>2. Web APIs</h4><p>When you encounter an asynchronous task (like a fetch request, a TIMER, or a DOM event), JavaScript offloads it to the <strong>Web API</strong> environment.</p><ul><li>This prevents the Call Stack from being blocked.</li><li>The Web API handles the “waiting” (e.g., waiting 5 seconds for a timer or waiting for a server response) in the background.</li></ul><h4>3. The Queues</h4><p>Once an asynchronous task is finished in the Web API, its callback is sent to one of two queues:</p><ul><li><strong>Microtask Queue:</strong> This has <strong>higher priority</strong>. It specifically holds callbacks from <strong>Promises</strong> (like .then or .catch).</li><li><strong>Macrotask Queue (Task Queue):</strong> This holds callbacks from things like setTimeout, setInterval, and DOM events</li></ul><h4>4. The Promise Object</h4><p>The diagram shows the internal state of a <strong>Promise Object</strong>:</p><ul><li><strong>State:</strong> It can be pending, fulfilled (as shown), or rejected.</li><li><strong>Result:</strong> Depending on the state, it triggers the corresponding callback: .then() for fulfillment or .catch() for rejection.</li></ul><h4>5. The Event Loop</h4><p>The <strong>Event Loop</strong> is the “manager” that connects the queues to the stack. It follows a specific logic:</p><ol><li>It checks if the <strong>Call Stack</strong> is empty.</li><li>If empty, it first empties the entire <strong>Microtask Queue</strong>.</li><li>Once the Microtasks are done, it takes <strong>one</strong> task from the <strong>Macrotask Queue</strong> and pushes it to the stack for execution.</li></ol><h3>The JavaScript Code Snippet</h3><pre>console.log(&quot;1. Stack Start&quot;); // Synchronous<br>// 2. A Microtask (Promise)<br>Promise.resolve()<br>  .then(() =&gt; {<br>    console.log(&quot;2. Promise Callback&quot;);<br>  });<br>// 3. A Macrotask (setTimeout with 0ms)<br>setTimeout(() =&gt; {<br>  console.log(&quot;3. Timer Callback&quot;);<br>}, 0);<br>console.log(&quot;4. Stack End&quot;); // Synchronous</pre><h3>The Visual Trace Diagram</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SRg0jN-_AaiBqzB8eMOnTg.png" /></figure><h3>Final Truth</h3><p>JavaScript is:</p><ul><li>Single-threaded</li><li>Synchronous by default</li><li>Asynchronous through environment support</li><li>Non-blocking via Event Loop</li></ul><p>It doesn’t multitask.</p><p>It delegates intelligently.</p><p>That’s why it scales.</p><p>That’s why it powers modern apps.</p><p>And that’s why understanding async JavaScript is a superpower for developers.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=502aa5c7e0a7" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[A Better Place to Put Your Python Virtual Environments]]></title>
            <link>https://medium.com/@shravanidalvi22/a-better-place-to-put-your-python-virtual-environments-a7a690704cfa?source=rss-34efbe7f857e------2</link>
            <guid isPermaLink="false">https://medium.com/p/a7a690704cfa</guid>
            <category><![CDATA[venv]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[django]]></category>
            <dc:creator><![CDATA[Shravanidalvi]]></dc:creator>
            <pubDate>Thu, 05 Mar 2026 11:04:50 GMT</pubDate>
            <atom:updated>2026-03-05T11:04:50.680Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/765/0*E2ljces3bEdpwfxZ.jpeg" /></figure><p>Virtual environments are vital if you’re developing Python apps or just writing some Python scripts. They allow you to isolate different app requirements to prevent conflicts and keep your global OS environment clean.</p><p>This is super important for many reasons. The most obvious one is requirements isolation. Let’s say that you’re working on two different projects:</p><ul><li>Project #1 requires Python 3.6 and the “requests” package version 2.1.0.</li><li>Project #2 requires Python 3.10 and requests 2.31.0.</li></ul><p>Although you can install both Python 3.6 and Python 3.10 globally on your OS, you may have to manually set the default version, which may end up breaking some packages in your OS that expect the other version.</p><p>As for other packages like &quot;requests,&quot; installing multiple versions of the same Python package globally is not possible.</p><p>The solution is to create a virtual environment, or &quot;venv,&quot; for each project, which will isolate it from the other environment and, most importantly, isolate it from your OS global environment.</p><p>Each venv has its own folder that contains all the packages and libraries the project needs to run correctly, including a copy of Python itself.</p><p>Then you can activate “venv1” when you’re working on Project #1. When it comes time to work on Project #2, you just deactivate “venv1” and activate &quot;venv2&quot;.</p><p>Better yet, you can have two separate terminal or IDE windows open at the same time, one for each project with its own “venv” activated.</p><p>Another great advantage of venvs is the ability to replicate the project’s environment on almost any computer, regardless of its OS or currently installed packages. This eliminates the popular “well, it runs on my laptop” excuse. Everybody can recreate the exact project environment using venvs and the requirements list saved in the “requirements.txt” file.</p><h3>The Common Approach: Creating venv Inside the Project Folder</h3><p>Most developers create their virtual environment inside the project directory. This approach has benefits — but also drawbacks.</p><h3>Benefits of Creating the “venv” Inside the Project Folder</h3><p><strong>1. Easier to create</strong><br>When you create a new project, you can simply dive cd into it and create the venv right there.</p><p><strong>2. Easier to activate</strong><br>You can activate it directly from the project folder using:</p><pre>source venv/bin/activate</pre><p>No need to remember where the venv lives on your disk.</p><p><strong>3. IDE optimization</strong><br>Most IDEs automatically detect a venv if it exists inside the project folder. Some even activate it automatically.</p><h3>Drawbacks of Creating the “venv” Inside the Project Folder</h3><h3>1. Makes Backups and Sync Slower</h3><p>Virtual environments can contain thousands of files and become very large.</p><p>If you use backup or sync systems such as NAS, Nextcloud, Dropbox, Google Drive, or similar tools, backing up venv folders can slow down the entire process.</p><p>Even if you try to exclude them, you might forget to add newly created projects to the ignore list. You usually notice only after backups start taking too long or storage fills up unexpectedly.</p><h3>The Solution: A Better Place for Your Virtual Environments</h3><p>Virtual environments are <strong>disposable by nature</strong>.</p><p>They contain packages downloaded from the internet and can be reproduced anytime using the requirements.txt file.</p><p>Because of this:</p><ul><li>They should not be backed up.</li><li>They should not be synced to cloud storage.</li><li>They do not need to be copied or moved around.</li><li>They can always be recreated.</li></ul><h3>Keep All Your Virtual Environments in a Central Disposable Folder</h3><p>The ideal place for venvs is:</p><ul><li>Outside the project folder</li><li>In a disposable location</li><li>In one central folder</li></ul><p>For example:</p><ul><li>~/Downloads/</li><li>/var/tmp/</li><li>/tmp/</li></ul><p>These locations are typically ignored by backup and sync tools.</p><p>Personally, I use:</p><pre>~/Downloads/virtual-envs/</pre><h3>Why Outside the Project Folder?</h3><p>Because:</p><ul><li>They won’t be included when you back up or sync your project.</li><li>Your project directory stays clean.</li><li>Only your actual source code and assets remain in the project.</li></ul><h3>Why a Disposable Folder?</h3><p>Because venvs are reproducible.</p><p>If deleted, you can recreate them with:</p><pre>pip install -r requirements.txt</pre><p>There is no need to protect them like personal documents or important source code.</p><h3>Why a Central Location?</h3><p>There are several advantages:</p><h3>1. Easy to Exclude from Backup</h3><p>Instead of excluding multiple venv folders individually, you only exclude one parent folder.</p><h3>2. Easy to Remember</h3><p>Instead of different locations for every project, you always know where your environments are stored.</p><p>Example:</p><pre>~/Downloads/virtual-envs/</pre><h3>Example Setup</h3><p>Here’s the setup I personally recommend:</p><h3>Step 1: Create the virtual environment in the central folder</h3><pre>python3 -m venv ~/Downloads/virtual-envs/project1</pre><h3>Step 2: Activate it</h3><p>Instead of typing the full path again, you can press:</p><pre>Alt + .</pre><p>This inserts the last argument from your previous command.</p><p>Then complete it like this:</p><pre>source ~/Downloads/virtual-envs/project1/bin/activate</pre><p>You can use Tab for auto-completion of bin and activate.</p><h3>Final Thought</h3><p>Virtual environments are temporary, reproducible, and disposable.</p><p>Treat them that way.</p><p>Keep them:</p><ul><li>Outside your project</li><li>In a disposable location</li><li>In one central folder</li></ul><p>Your system will stay cleaner, backups will be faster, and your workflow will be more organized.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a7a690704cfa" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>