<?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>Forem: Aakash Kumar Chaudhary</title>
    <description>The latest articles on Forem by Aakash Kumar Chaudhary (@aakash176).</description>
    <link>https://forem.com/aakash176</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1199075%2Fbd5384ca-6555-4536-adf0-59b8a4274c7c.jpeg</url>
      <title>Forem: Aakash Kumar Chaudhary</title>
      <link>https://forem.com/aakash176</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/aakash176"/>
    <language>en</language>
    <item>
      <title>Understanding async and await in JavaScript: The Key to Cleaner Asynchronous Code</title>
      <dc:creator>Aakash Kumar Chaudhary</dc:creator>
      <pubDate>Sat, 14 Dec 2024 06:53:16 +0000</pubDate>
      <link>https://forem.com/aakash176/understanding-async-and-await-in-javascript-the-key-to-cleaner-asynchronous-code-583n</link>
      <guid>https://forem.com/aakash176/understanding-async-and-await-in-javascript-the-key-to-cleaner-asynchronous-code-583n</guid>
      <description>&lt;p&gt;JavaScript's asynchronous nature is one of its greatest strengths, but it can also be a source of frustration for developers. Over time, we’ve moved from callback functions (and the dreaded "callback hell") to promises, and now to async and await. These modern tools simplify asynchronous programming, making your code more readable, maintainable, and efficient.&lt;/p&gt;

&lt;p&gt;But how do async and await really work, and why are they so beneficial? Let’s dive deep and explore!&lt;/p&gt;

&lt;p&gt;What Are &lt;strong&gt;async&lt;/strong&gt; and &lt;strong&gt;await&lt;/strong&gt;?&lt;br&gt;
In JavaScript, async and await are part of ES2017 (ES8) and provide a way to handle asynchronous operations in a more synchronous-looking manner. They are built on top of Promises and allow you to write cleaner, more readable code.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;em&gt;async&lt;/em&gt;&lt;/strong&gt;: Declaring a function as async ensures that it always returns a promise, even if you don’t explicitly return one&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;em&gt;await&lt;/em&gt;&lt;/strong&gt;: This keyword is used inside async functions to pause the execution of the function until a promise is resolved or rejected. It makes asynchronous code appear synchronous.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;How Do They Work?&lt;/strong&gt;&lt;br&gt;
Let’s see some examples to understand how they work.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;async&lt;/em&gt; &lt;strong&gt;Function Example&lt;/strong&gt;&lt;/p&gt;

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

&lt;p&gt;Here, the &lt;strong&gt;async&lt;/strong&gt; keyword automatically wraps the return value in a promise. So, calling &lt;strong&gt;&lt;em&gt;sayHello()&lt;/em&gt;&lt;/strong&gt; will return a promise that resolves to &lt;em&gt;&lt;strong&gt;"Hello, World!"&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Using &lt;strong&gt;await&lt;/strong&gt; in an &lt;strong&gt;async&lt;/strong&gt; Function&lt;br&gt;
The &lt;strong&gt;await&lt;/strong&gt; keyword pauses the execution of the function until the promise is resolved or rejected.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjhmhx1j36hxjz6afnvoh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjhmhx1j36hxjz6afnvoh.png" alt="snapshot of code" width="800" height="132"&gt;&lt;/a&gt;&lt;br&gt;
Without &lt;strong&gt;await&lt;/strong&gt;, you’d need to chain &lt;strong&gt;.then()&lt;/strong&gt; calls, which can make the code harder to read.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Does&lt;/strong&gt; &lt;em&gt;await&lt;/em&gt; &lt;strong&gt;Block the Execution?&lt;/strong&gt;&lt;br&gt;
If &lt;strong&gt;await&lt;/strong&gt; really blocks the execution then how it is different than traditional synchronous JavaScript and what is the actual benefit of using it?&lt;/p&gt;

&lt;p&gt;Here’s where some confusion arises: await &lt;strong&gt;does NOT block the main thread&lt;/strong&gt;. It only pauses the execution of the current async function, while other parts of your program continue to run.&lt;/p&gt;

&lt;p&gt;Analogy:&lt;br&gt;
Imagine you're cooking:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Synchronous&lt;/strong&gt;: You have to stand at the stove and stir the pot constantly until the food is cooked. You can't do anything else during that time.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Async/Await&lt;/strong&gt;: You put the food in the oven (await a Promise that it will be cooked). You can then do other things (chop vegetables, set the table) while you wait. When the timer rings (the Promise resolves), you go back to the oven and take out the food.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

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

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

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcgm7hfuusj5intdfq8r5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcgm7hfuusj5intdfq8r5.png" alt="snapshot of output" width="641" height="254"&gt;&lt;/a&gt;&lt;br&gt;
Notice that "End" is logged before "Data fetched." This demonstrates that while &lt;strong&gt;await&lt;/strong&gt; pauses the &lt;strong&gt;fetchData&lt;/strong&gt; function, it doesn’t block the rest of the program.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;br&gt;
&lt;em&gt;async&lt;/em&gt; and &lt;em&gt;await&lt;/em&gt; are powerful tools that transform how we write asynchronous code in JavaScript. By making asynchronous workflows look synchronous, they improve readability, simplify error handling, and give you precise control over sequential and parallel operations—all without blocking the main thread.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
    <item>
      <title>Implementing infinite scrolling in react</title>
      <dc:creator>Aakash Kumar Chaudhary</dc:creator>
      <pubDate>Wed, 09 Oct 2024 19:20:30 +0000</pubDate>
      <link>https://forem.com/aakash176/implementing-infinite-scrolling-in-react-5gj8</link>
      <guid>https://forem.com/aakash176/implementing-infinite-scrolling-in-react-5gj8</guid>
      <description>&lt;p&gt;Before we start coding, if you want to know more about what pagination is and why do we need it, do check out my blog &lt;a href="https://dev.to/aakash176/pagination-in-react-8p9"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Let's say the ask is to display 50 items on the screen and whenever user reaches to the bottom, load next 50 items.&lt;br&gt;
For that we need to keep track of the scroll position and keep on comparing it with the document body offsetHeight.&lt;/p&gt;

&lt;p&gt;To get the scroll position, we will use &lt;strong&gt;window.scrollY&lt;/strong&gt;.&lt;br&gt;
Basically &lt;strong&gt;window.scrollY&lt;/strong&gt; gives the number of pixels the page has been scrolled vertically from the top. It tells you how far down the page the user has scrolled.&lt;br&gt;
Along with the window.scrollY, we will be using two more values to check if the user has reached at the bottom of the page or not.&lt;br&gt;
&lt;strong&gt;window.innerHeight:&lt;/strong&gt; This represents the height of the visible part of the window (the viewport). It's the height of the area that the user can currently see in the browser without scrolling.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;document.body.offsetHeight:&lt;/strong&gt; It gives the total height of the body element.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { useState, useEffect } from "react";
import "./styles.css";
export default function App() {
  let query = [];
  let [items, setitems] = useState(50);
  for (let i = 1; i &amp;lt;= items; i++) {
    query.push(&amp;lt;p&amp;gt;{i}&amp;lt;/p&amp;gt;);
  }

  useEffect(() =&amp;gt; {
    const onScroll = () =&amp;gt; {
      if (
        window.innerHeight + window.scrollY &amp;gt;=
        document.body.offsetHeight - 40
      ) {
        setitems(items + 50);
      }
    };

    window.addEventListener("scroll", onScroll);
    return () =&amp;gt; window.removeEventListener("scroll", onScroll);
  }, [items]);

  return &amp;lt;div className="App"&amp;gt;{query.map((q) =&amp;gt; q)}&amp;lt;/div&amp;gt;;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Explanation&lt;/strong&gt;: The first render of the page will trigger the useEffect hook which will add a scroll event. Whenever scrolling event happens, onScroll() method will be invoked and the it will check the position of the scroll. If it is at the bottom-40, then more 50 items is added to the items state.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>programming</category>
      <category>react</category>
    </item>
    <item>
      <title>Implementing infinite scrolling in react</title>
      <dc:creator>Aakash Kumar Chaudhary</dc:creator>
      <pubDate>Wed, 09 Oct 2024 19:20:30 +0000</pubDate>
      <link>https://forem.com/aakash176/implementing-infinite-scrolling-in-react-4m2f</link>
      <guid>https://forem.com/aakash176/implementing-infinite-scrolling-in-react-4m2f</guid>
      <description>&lt;p&gt;Before we start coding, if you want to know more about what pagination is and why do we need it, do check out my blog &lt;a href="https://dev.to/aakash176/pagination-in-react-8p9"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Let's say the ask is to display 50 items on the screen and whenever user reaches to the bottom, load next 50 items.&lt;br&gt;
For that we need to keep track of the scroll position and keep on comparing it with the document body offsetHeight.&lt;/p&gt;

&lt;p&gt;To get the scroll position, we will use &lt;strong&gt;window.scrollY&lt;/strong&gt;.&lt;br&gt;
Basically &lt;strong&gt;window.scrollY&lt;/strong&gt; gives the number of pixels the page has been scrolled vertically from the top. It tells you how far down the page the user has scrolled.&lt;br&gt;
Along with the window.scrollY, we will be using two more values to check if the user has reached at the bottom of the page or not.&lt;br&gt;
&lt;strong&gt;window.innerHeight:&lt;/strong&gt; This represents the height of the visible part of the window (the viewport). It's the height of the area that the user can currently see in the browser without scrolling.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;document.body.offsetHeight:&lt;/strong&gt; It gives the total height of the body element.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { useState, useEffect } from "react";
import "./styles.css";
export default function App() {
  let query = [];
  let [items, setitems] = useState(50);
  for (let i = 1; i &amp;lt;= items; i++) {
    query.push(&amp;lt;p&amp;gt;{i}&amp;lt;/p&amp;gt;);
  }

  useEffect(() =&amp;gt; {
    const onScroll = () =&amp;gt; {
      if (
        window.innerHeight + window.scrollY &amp;gt;=
        document.body.offsetHeight - 40
      ) {
        setitems(items + 50);
      }
    };

    window.addEventListener("scroll", onScroll);
    return () =&amp;gt; window.removeEventListener("scroll", onScroll);
  }, [items]);

  return &amp;lt;div className="App"&amp;gt;{query.map((q) =&amp;gt; q)}&amp;lt;/div&amp;gt;;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Explanation&lt;/strong&gt;: The first render of the page will trigger the useEffect hook which will add a scroll event. Whenever scrolling event happens, onScroll() method will be invoked and the it will check the position of the scroll. If it is at the bottom-40, then more 50 items is added to the items state.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>programming</category>
      <category>react</category>
    </item>
    <item>
      <title>Pagination in React</title>
      <dc:creator>Aakash Kumar Chaudhary</dc:creator>
      <pubDate>Wed, 09 Oct 2024 18:42:44 +0000</pubDate>
      <link>https://forem.com/aakash176/pagination-in-react-8p9</link>
      <guid>https://forem.com/aakash176/pagination-in-react-8p9</guid>
      <description>&lt;p&gt;&lt;strong&gt;What is Pagination anyway?&lt;/strong&gt;&lt;br&gt;
Pagination is the process of dividing a large set of data or content into separate, smaller, sequentially numbered pages, making it easier for users to navigate and access the content.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why do we need Pagination?&lt;/strong&gt;&lt;br&gt;
Pagination is important for several reasons, especially when handling large data sets or extensive content on websites or applications. Here are the main reasons why we need pagination:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Improves Performance:&lt;/strong&gt;&lt;br&gt;
Loading large amounts of data at once can be slow and resource-intensive for both the server and the client. Pagination helps break the data into smaller chunks, reducing the load on the server and improving page load times.&lt;br&gt;
&lt;strong&gt;2. Enhances User Experience:&lt;/strong&gt;&lt;br&gt;
Showing too much content at once can overwhelm users and make it hard to find relevant information. Pagination provides a structured way to navigate through content, improving usability and making it easier for users to find what they need.&lt;br&gt;
&lt;strong&gt;3. Reduces Bandwidth Usage&lt;/strong&gt;:&lt;br&gt;
For users on slower networks or with limited data, loading large amounts of content all at once can consume significant bandwidth. Pagination allows only a small portion of the data to be loaded initially, reducing bandwidth consumption.&lt;br&gt;
&lt;strong&gt;4. Organizes Content:&lt;/strong&gt;&lt;br&gt;
Pagination helps organize content by breaking it into logical sections, making it easier to display and browse through data incrementally, especially on platforms like blogs, e-commerce websites, or search results pages.&lt;br&gt;
&lt;strong&gt;5. Simplifies Navigation:&lt;/strong&gt;&lt;br&gt;
Pagination provides clear navigation controls (e.g., page numbers, "Next" and "Previous" buttons), allowing users to easily jump to specific pages of content without scrolling through endless amounts of data.&lt;br&gt;
&lt;strong&gt;6. Improves Server Efficiency:&lt;/strong&gt;&lt;br&gt;
Handling requests for smaller chunks of data instead of retrieving the entire data set can improve server efficiency and scalability, especially in applications where many users are requesting data simultaneously.&lt;br&gt;
&lt;strong&gt;7. Better for SEO (Search Engine Optimization):&lt;/strong&gt;&lt;br&gt;
Search engines can crawl and index paginated content more effectively. Breaking content into smaller, structured pieces with clear pagination makes it easier for search engines to understand and rank each part of the content.&lt;br&gt;
&lt;strong&gt;When Is Pagination Needed?&lt;/strong&gt;&lt;br&gt;
When displaying a long list of items (e.g., products, search results).&lt;br&gt;
When dealing with large data sets that would cause performance issues if loaded all at once.&lt;br&gt;
When you want to give users more control over how much data they view at once.&lt;br&gt;
In summary, pagination is essential for handling large data sets efficiently, improving user experience, enhancing performance, and organizing content in a way that's both easy to navigate and scalable.&lt;/p&gt;

</description>
      <category>react</category>
      <category>webdev</category>
      <category>javascript</category>
      <category>html</category>
    </item>
  </channel>
</rss>
