<?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: Md Mafiz Uddin</title>
    <description>The latest articles on DEV Community by Md Mafiz Uddin (@pacehut).</description>
    <link>https://dev.to/pacehut</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%2F919873%2F7428858c-b251-41ef-aa40-d88bc8728852.jpeg</url>
      <title>DEV Community: Md Mafiz Uddin</title>
      <link>https://dev.to/pacehut</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/pacehut"/>
    <language>en</language>
    <item>
      <title>What is MCP? A Simple Guide to AI’s Next Big Thing</title>
      <dc:creator>Md Mafiz Uddin</dc:creator>
      <pubDate>Wed, 19 Mar 2025 17:52:02 +0000</pubDate>
      <link>https://dev.to/pacehut/what-is-mcp-a-simple-guide-to-ais-next-big-thing-kba</link>
      <guid>https://dev.to/pacehut/what-is-mcp-a-simple-guide-to-ais-next-big-thing-kba</guid>
      <description>&lt;h1&gt;
  
  
  Imagine a World Where AI Integrates Effortlessly
&lt;/h1&gt;

&lt;p&gt;Meet &lt;strong&gt;MCP (Model Context Protocol)&lt;/strong&gt;—an open standard that might be the game-changer developers and innovators have been dreaming of.  &lt;/p&gt;

&lt;p&gt;Pioneered by &lt;strong&gt;Anthropic’s&lt;/strong&gt; bold vision, MCP tackles a core LLM limitation: They’re brilliant but hands-off. They’ll write you an essay but won’t email it, craft a database query but won’t hit “run.” MCP rewires that with &lt;strong&gt;context&lt;/strong&gt;—unlocking a new level of AI capability.  &lt;/p&gt;

&lt;h2&gt;
  
  
  How MCP Works
&lt;/h2&gt;

&lt;p&gt;MCP’s power is in its &lt;strong&gt;context&lt;/strong&gt;. It’s a framework built on four pillars: &lt;strong&gt;resources, tools, prompts, and sampling&lt;/strong&gt;. Here’s the real scoop:  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Resources&lt;/strong&gt;: Your files, databases, or live feeds—like GitHub repos or Slack channels. MCP servers tap these, giving AI a window into your world.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Tools&lt;/strong&gt;: The magic sauce—executable functions. Think &lt;em&gt;“fix this code”&lt;/em&gt; or &lt;em&gt;“post this update.”&lt;/em&gt; The better the tools, the stronger the MCP server. Quality here is make-or-break.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Prompts&lt;/strong&gt;: Smart, pre-set guides that keep AI on track—think of them as the AI’s cheat sheet. A server with sharp prompts means sharper results.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Sampling&lt;/strong&gt;: The wild card—leveraging multiple LLMs to run different tools, blending outputs for richer solutions. It’s like an AI brainstorming session.
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Why It’s a Big Deal
&lt;/h2&gt;

&lt;p&gt;MCP’s strength hinges on its &lt;strong&gt;servers&lt;/strong&gt;. A top-tier server with ace tools and prompts turns AI into a &lt;strong&gt;doer, not just a thinker&lt;/strong&gt;. A shaky one? You’re stuck with suggestions, not solutions.  &lt;/p&gt;

&lt;p&gt;And here’s the kicker: It’s not just for big players like &lt;strong&gt;Cursor&lt;/strong&gt; or &lt;strong&gt;Windsurf&lt;/strong&gt;. The race is wide open—new tools will emerge, and &lt;strong&gt;you&lt;/strong&gt; could build one.  &lt;/p&gt;

&lt;h3&gt;
  
  
  Picture This
&lt;/h3&gt;

&lt;p&gt;Your AI:&lt;br&gt;&lt;br&gt;
✅ Dives into your &lt;strong&gt;GitHub repo&lt;/strong&gt;, spots a pesky bug in your code&lt;br&gt;&lt;br&gt;
✅ Fixes it using a &lt;strong&gt;tool&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
✅ Checks your &lt;strong&gt;Slack channel&lt;/strong&gt; for past chatter about the issue&lt;br&gt;&lt;br&gt;
✅ Uses &lt;strong&gt;sampling&lt;/strong&gt; across models to validate the best fix  &lt;/p&gt;

&lt;p&gt;That’s &lt;strong&gt;MCP flexing its muscles&lt;/strong&gt;.  &lt;/p&gt;

&lt;h2&gt;
  
  
  MCP vs. APIs
&lt;/h2&gt;

&lt;p&gt;🔹 &lt;strong&gt;APIs&lt;/strong&gt; are static, one-trick ponies.&lt;br&gt;&lt;br&gt;
🔹 &lt;strong&gt;MCP&lt;/strong&gt; builds a &lt;strong&gt;living ecosystem&lt;/strong&gt; where &lt;strong&gt;context drives action&lt;/strong&gt;—and anyone can join the party.  &lt;/p&gt;

&lt;p&gt;The catch? It’s &lt;strong&gt;only as good as the servers you craft&lt;/strong&gt;.  &lt;/p&gt;

&lt;h2&gt;
  
  
  Explore More
&lt;/h2&gt;

&lt;p&gt;MCP is &lt;strong&gt;open-source&lt;/strong&gt; and yours to shape: &lt;a href="https://modelcontextprotocol.io" rel="noopener noreferrer"&gt;modelcontextprotocol.io&lt;/a&gt;  &lt;/p&gt;

&lt;h3&gt;
  
  
  What’s Your Take?
&lt;/h3&gt;

&lt;p&gt;How could MCP’s context and tools reshape your workflow? What killer MCP server would &lt;strong&gt;you&lt;/strong&gt; build?  &lt;/p&gt;

&lt;p&gt;Let’s hash it out in the comments! 🚀  &lt;/p&gt;

&lt;p&gt;Before We End...&lt;/p&gt;

&lt;p&gt;That's all for now. I hope you've found this helpful&lt;br&gt;
:) Happy streaming!&lt;/p&gt;

&lt;p&gt;Regards: Md Mafizuddin&lt;/p&gt;

&lt;p&gt;For any queries connect with me: LinkedIn: &lt;a href="https://www.linkedin.com/in/mafizuddin/" rel="noopener noreferrer"&gt;@mdmafizuddin&lt;/a&gt; Twitter: &lt;a href="https://twitter.com/whyknowmee" rel="noopener noreferrer"&gt;@whyknowmee&lt;/a&gt; Github: &lt;a href="https://github.com/pacehutt" rel="noopener noreferrer"&gt;@pacehut&lt;/a&gt; Instagram: &lt;a href="https://www.instagram.com/mafiz._/" rel="noopener noreferrer"&gt;@mafiz._&lt;/a&gt;&lt;/p&gt;

</description>
      <category>ai</category>
      <category>mcp</category>
      <category>openai</category>
      <category>mcpservers</category>
    </item>
    <item>
      <title>HTTP Live Streaming: An In-Depth Look</title>
      <dc:creator>Md Mafiz Uddin</dc:creator>
      <pubDate>Fri, 25 Oct 2024 13:32:04 +0000</pubDate>
      <link>https://dev.to/pacehut/hls-demystified-an-in-depth-look-at-http-live-streaming-5dg</link>
      <guid>https://dev.to/pacehut/hls-demystified-an-in-depth-look-at-http-live-streaming-5dg</guid>
      <description>&lt;p&gt;&lt;strong&gt;HTTP Live Streaming (HLS)&lt;/strong&gt; has transformed video delivery, enabling smooth, adaptable playback across diverse devices and network conditions. Created by Apple, this powerful protocol allows users to experience video without buffering, regardless of device or internet quality, making it a go-to solution for online video streaming. In this guide, we’ll break down what HLS is, how it works, and the technical foundations that make it so effective.&lt;/p&gt;




&lt;h2&gt;
  
  
  Table of Contents
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;What is HLS?&lt;/li&gt;
&lt;li&gt;How Does HLS Work?&lt;/li&gt;
&lt;li&gt;HLS Components and Process Flow&lt;/li&gt;
&lt;li&gt;Adaptive Bitrate Streaming Explained&lt;/li&gt;
&lt;li&gt;Benefits of HLS&lt;/li&gt;
&lt;li&gt;Comparing HLS to Other Protocols&lt;/li&gt;
&lt;li&gt;Real-World Use Cases for HLS&lt;/li&gt;
&lt;li&gt;The Future of HLS&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  What is HLS?
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;HTTP Live Streaming (HLS)&lt;/strong&gt; is a video streaming protocol used to transfer media over the internet by breaking content into small, sequential chunks. It enables video content to adapt seamlessly to changing network speeds by offering different quality levels that can adjust dynamically during playback.&lt;/p&gt;

&lt;p&gt;Contrary to what its name suggests, HLS is not limited to live content—it's commonly used for on-demand videos as well. The technology behind HLS allows for a continuous and high-quality experience across all devices, whether it’s a smartphone, laptop, or television.&lt;/p&gt;




&lt;h2&gt;
  
  
  How Does HLS Work?
&lt;/h2&gt;

&lt;p&gt;An HLS stream originates from a server where (in on-demand streaming) the media file is stored, or where (in live streaming) the stream is created. Because HLS is based on HTTP, any ordinary web server can originate the stream.&lt;/p&gt;

&lt;p&gt;HLS achieves smooth playback and adaptive quality by dividing the video into smaller parts called &lt;strong&gt;segments&lt;/strong&gt; or &lt;strong&gt;chunks&lt;/strong&gt;. These chunks are generally between 2-10 seconds each and are stored as individual files on the server. Here’s an overview of how it works:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Encoding and Segmenting&lt;/strong&gt;: The original video file is encoded at multiple bitrates and resolutions. Each version is then segmented into smaller parts, which makes it easier to handle the video in short, digestible pieces.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Playlist Files (Manifests)&lt;/strong&gt;: HLS uses &lt;code&gt;.m3u8&lt;/code&gt; files (known as &lt;strong&gt;playlists&lt;/strong&gt; or &lt;strong&gt;manifests&lt;/strong&gt;) to keep track of these segments. The playlists direct the player on which segments to download and in what order. Two types of playlists are commonly used:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Master Playlist&lt;/strong&gt;: Points to various quality levels available for playback.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Variant Playlists&lt;/strong&gt;: Each points to chunks of a specific quality level.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Dynamic Quality Selection&lt;/strong&gt;: As the viewer’s device streams the video, the player continuously assesses the available network bandwidth and chooses the optimal quality level for that moment.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;HTTP Delivery&lt;/strong&gt;: Since HLS segments are delivered over HTTP, they are compatible with Content Delivery Networks (CDNs), which helps scale the delivery to global audiences.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  HLS Components and Process Flow
&lt;/h2&gt;

&lt;p&gt;To fully grasp how HLS operates, let’s break down the key components and the process flow.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. &lt;strong&gt;Encoding Multiple Qualities&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Encoding is the first step in HLS. The original video is converted into several versions at varying resolutions and bitrates (e.g., 240p at 300kbps, 480p at 800kbps, 720p at 1500kbps). This encoding process creates multiple versions suited for different network conditions.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. &lt;strong&gt;Segmenting the Video&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;The encoded video is split into shorter segments. For instance, a 10-minute video could be divided into 5-second chunks. This segmentation is key to achieving &lt;strong&gt;adaptive bitrate streaming&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. &lt;strong&gt;Playlist (Manifest) Files&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;The playlist files organize the segments and provide instructions to the player:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;strong&gt;Master Playlist (&lt;code&gt;master.m3u8&lt;/code&gt;)&lt;/strong&gt; contains links to each quality level.&lt;/li&gt;
&lt;li&gt;The &lt;strong&gt;Variant Playlist (&lt;code&gt;playlist.m3u8&lt;/code&gt;)&lt;/strong&gt; lists the specific segments for a particular quality level. Each variant playlist contains URLs for the individual video chunks.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  4. &lt;strong&gt;Adaptive Streaming Process&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;When a user initiates playback, the player downloads the master playlist to access all available quality levels and chooses a variant playlist based on network speed. If the connection weakens, the player switches to a lower-quality variant playlist seamlessly.&lt;/p&gt;

&lt;h3&gt;
  
  
  5. &lt;strong&gt;Delivery Over HTTP&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Each chunk is delivered over standard HTTP, which ensures compatibility with CDN caching for scalability and improved performance. &lt;/p&gt;




&lt;h2&gt;
  
  
  Adaptive Bitrate Streaming Explained
&lt;/h2&gt;

&lt;p&gt;Adaptive Bitrate Streaming (ABR) is a core feature of HLS. The player continuously monitors network conditions and device capabilities, allowing it to switch between quality levels in real-time. Let’s break down how ABR works in a live scenario:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Initial Playback&lt;/strong&gt;: When playback begins, the player selects a quality level that matches the current network speed.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Network Changes&lt;/strong&gt;: If the connection speed drops, the player switches to a lower bitrate (e.g., from 720p to 480p) to maintain smooth playback. &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Quality Restoration&lt;/strong&gt;: As the connection improves, the player reverts to a higher-quality version.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;ABR makes it possible for viewers to watch content without needing to manually adjust quality, enhancing the overall experience.&lt;/p&gt;




&lt;h2&gt;
  
  
  Benefits of HLS
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Broad Compatibility&lt;/strong&gt;: HLS works across major operating systems, including iOS, Android, and most browsers, thanks to its reliance on HTTP.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Adaptive Streaming&lt;/strong&gt;: HLS adjusts video quality dynamically, ideal for mobile users and others on variable connections.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Scalability with HTTP and CDNs&lt;/strong&gt;: Since HLS uses HTTP, it scales easily using CDN caching, allowing providers to deliver video to global audiences with reduced latency.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Security&lt;/strong&gt;: HLS supports encryption and secure streaming through HTTPS, making it more secure than older protocols.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  Comparing HLS to Other Protocols
&lt;/h2&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;HLS vs. MPEG-DASH&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Compatibility&lt;/strong&gt;: HLS has broader support on Apple devices, while MPEG-DASH is codec-agnostic and works better in some modern, cross-platform environments.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Flexibility&lt;/strong&gt;: MPEG-DASH allows for a wider range of codecs, while HLS often relies on H.264.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;HLS vs. RTMP&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Scalability&lt;/strong&gt;: RTMP is an older protocol, and its reliance on Flash makes it less suitable for modern use. HLS’s compatibility with HTTP/CDNs makes it far more scalable.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Latency&lt;/strong&gt;: RTMP has lower latency than HLS, which can be beneficial in some real-time applications.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Real-World Use Cases for HLS
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Live Sports&lt;/strong&gt;: Real-time, adaptive video for viewers on mobile networks.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;News Broadcasting&lt;/strong&gt;: HLS ensures reliable playback on any device.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Education&lt;/strong&gt;: Adaptive bitrate keeps online classes smooth and accessible to students on varied connections.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  The Future of HLS
&lt;/h2&gt;

&lt;p&gt;As internet access improves and streaming becomes increasingly central to online experiences, protocols like HLS will continue to adapt and expand. Emerging features, such as &lt;strong&gt;Low-Latency HLS (LL-HLS)&lt;/strong&gt;, are reducing delays, further enhancing HLS’s applicability for live and interactive content.&lt;/p&gt;




&lt;p&gt;In short, &lt;strong&gt;HLS is one of the most powerful tools for delivering adaptive, scalable, and high-quality video&lt;/strong&gt;. Its HTTP-based delivery, combined with adaptive bitrate streaming, makes it ideal for a wide range of applications, from entertainment to education. By demystifying the inner workings of HLS, we gain a deeper appreciation for how this technology enhances our everyday streaming experiences.&lt;/p&gt;

&lt;h2&gt;
  
  
  Before We End...
&lt;/h2&gt;

&lt;p&gt;That's all for now. I hope you've found this helpful&lt;br&gt;
Happy Streaming :)&lt;/p&gt;

&lt;p&gt;Regards:&lt;br&gt;
Md Mafizuddin&lt;/p&gt;

&lt;p&gt;For any queries connect with me:&lt;br&gt;
LinkedIn: &lt;a href="https://www.linkedin.com/in/mafizonly/" rel="noopener noreferrer"&gt;@mafizonly&lt;/a&gt;&lt;br&gt;
Twitter: &lt;a href="https://twitter.com/whyknowmee" rel="noopener noreferrer"&gt;@whyknowmee&lt;/a&gt;&lt;br&gt;
Github: &lt;a href="https://github.com/pacehutt" rel="noopener noreferrer"&gt;@pacehut&lt;/a&gt;&lt;br&gt;
Instagram: &lt;a href="https://www.instagram.com/mafiz._/" rel="noopener noreferrer"&gt;@mafiz._&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>How to create a Browser Extension?</title>
      <dc:creator>Md Mafiz Uddin</dc:creator>
      <pubDate>Wed, 22 Feb 2023 13:39:30 +0000</pubDate>
      <link>https://dev.to/pacehut/how-to-create-a-browser-extension-5819</link>
      <guid>https://dev.to/pacehut/how-to-create-a-browser-extension-5819</guid>
      <description>&lt;p&gt;Google Chrome is a popular web browser that allows users to customize their browsing experience with extensions. Chrome extensions are small software programs that add functionality to the browser. They can modify and enhance the appearance and functionality of web pages, manage downloads, block ads, and more. In this blog, we will learn how to create a simple Chrome extension that can help users manage their to-do list. We will also add a nice design to the extension using CSS and neo-brutalism design UI.&lt;br&gt;
.&lt;br&gt;
.&lt;br&gt;
.&lt;/p&gt;
&lt;h3&gt;
  
  
  Step 1: Setting up the Chrome Extension
&lt;/h3&gt;

&lt;p&gt;To create a Chrome extension, we first need to create a new folder and name it whatever we like. In this example, let's name it "todo-extension". Inside this folder, we will create the following files:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;manifest.json&lt;/li&gt;
&lt;li&gt;popup.html&lt;/li&gt;
&lt;li&gt;popup.js&lt;/li&gt;
&lt;li&gt;popup.css
.
.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The manifest.json file is a configuration file that provides information about the extension, such as the name, version, and permissions. Here is an example of a basic manifest.json file for our to-do list extension:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"manifest_version"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"To-do List"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"version"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"1.0"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"description"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"A simple to-do list extension"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;

    &lt;/span&gt;&lt;span class="nl"&gt;"action"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"default_popup"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"popup.html"&lt;/span&gt;&lt;span class="w"&gt;

    &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"permissions"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"storage"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;


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

&lt;/div&gt;



&lt;p&gt;In the &lt;code&gt;manifest.json&lt;/code&gt; file, we specify the name, version, and description of the extension. We also specify the icons that will be used for the extension, and the &lt;code&gt;permissions&lt;/code&gt;required to use the extension. In this case, we need permission to access the &lt;strong&gt;browser storage&lt;/strong&gt;.&lt;br&gt;
.&lt;br&gt;
.&lt;/p&gt;
&lt;h3&gt;
  
  
  Step 2: Creating the UI
&lt;/h3&gt;

&lt;p&gt;In the &lt;code&gt;popup.html&lt;/code&gt; file, we will create the UI for the to-do list extension. This file will be displayed when the user clicks on the extension icon in the Chrome toolbar. Here is an example of the code for the popup.html file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;!DOCTYPE html&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;meta&lt;/span&gt; &lt;span class="na"&gt;charset=&lt;/span&gt;&lt;span class="s"&gt;"utf-8"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;title&amp;gt;&lt;/span&gt;To-do List&lt;span class="nt"&gt;&amp;lt;/title&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;link&lt;/span&gt; &lt;span class="na"&gt;rel=&lt;/span&gt;&lt;span class="s"&gt;"stylesheet"&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"text/css"&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"popup.css"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;link&lt;/span&gt;
      &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"https://fonts.googleapis.com/css2?family=Open+Sans:wght@300;400;600&amp;amp;display=swap"&lt;/span&gt;
      &lt;span class="na"&gt;rel=&lt;/span&gt;&lt;span class="s"&gt;"stylesheet"&lt;/span&gt;
    &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"container"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;h1&amp;gt;&lt;/span&gt;To-do List&lt;span class="nt"&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;form&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"todo-form"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"text"&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"todo-input"&lt;/span&gt; &lt;span class="na"&gt;placeholder=&lt;/span&gt;&lt;span class="s"&gt;"Add a new todo..."&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;button&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"submit"&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"add-todo"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Add&lt;span class="nt"&gt;&amp;lt;/button&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;/form&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;ul&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"todo-list"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/ul&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"popup.js"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;.&lt;br&gt;
.&lt;/p&gt;

&lt;p&gt;**&lt;/p&gt;



&lt;p&gt;Here's the CSS code which I guess is not relevant to our topic and you must have done this simple designing till now.&lt;br&gt;
**&lt;/p&gt;

&lt;p&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;body&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;background-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;#262626&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;#fff&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;font-family&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;"Open Sans"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Arial&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;sans-serif&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;font-size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;14px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;margin&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;padding&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;#container&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;max-width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;350px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;margin&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;20px&lt;/span&gt; &lt;span class="nb"&gt;auto&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;padding&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;10px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;box-sizing&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;border-box&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;max-height&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;400px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;overflow-y&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;auto&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;#container&lt;/span&gt;&lt;span class="nd"&gt;::-webkit-scrollbar&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;6px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;background-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;#f1f1f1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;#container&lt;/span&gt;&lt;span class="nd"&gt;::-webkit-scrollbar-thumb&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;background-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;#888&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;border-radius&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;3px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nt"&gt;h1&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;text-align&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;center&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;margin-bottom&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;20px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nt"&gt;form&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;display&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;flex&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;flex-direction&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;column&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;margin-bottom&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;20px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nt"&gt;input&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="nt"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;"text"&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;padding&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;10px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;font-size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;14px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;border&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;none&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;border-radius&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;3px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;outline&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;none&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;box-shadow&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;none&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;background-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;#fff&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;#000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;margin-bottom&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;10px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nt"&gt;button&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="nt"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;"submit"&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;padding&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;10px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;font-size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;14px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;border&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;none&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;border-radius&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;3px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;outline&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;none&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;box-shadow&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;none&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;background-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;#4285f4&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;#fff&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;cursor&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;pointer&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;margin-top&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;10px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nt"&gt;button&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="nt"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;"submit"&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="nd"&gt;:hover&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;background-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;#3367d6&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nt"&gt;ul&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;list-style&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;none&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;margin&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;padding&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nt"&gt;li&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;padding&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;10px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;font-size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;14px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;background-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;#eee&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;border-radius&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;3px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;margin-bottom&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;10px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;#000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nt"&gt;li&lt;/span&gt;&lt;span class="nd"&gt;:last-child&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;margin-bottom&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0&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;br&gt;
.&lt;/p&gt;
&lt;h3&gt;
  
  
  Step 3: Adding Functionality with JavaScript
&lt;/h3&gt;

&lt;p&gt;Now we will add the functionality to the extension using JavaScript. In the &lt;code&gt;popup.js&lt;/code&gt; file, we will write the code that will handle the user input and save the &lt;code&gt;todos&lt;/code&gt;to the browser storage. &lt;/p&gt;

&lt;p&gt;First, we will get the form and input field using their ids. Then we will add an event listener to the form that will trigger when the user submits the form. In the event listener, we will prevent the default form submission behavior, get the value of the input field, clear the input field, add the todo item to the list, and save the todo to the browser storage. We will also define two helper functions: &lt;code&gt;addTodoToList&lt;/code&gt;and &lt;code&gt;saveTodoToStorage&lt;/code&gt;. The &lt;code&gt;addTodoToList&lt;/code&gt;function will add the todo item to the unordered list &lt;code&gt;(ul)&lt;/code&gt; in the UI, and the &lt;code&gt;saveTodoToStorage&lt;/code&gt;function will save the todo item to the browser &lt;code&gt;storage&lt;/code&gt;.&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="c1"&gt;// get the form and input field&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;form&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;todo-form&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;input&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;todo-input&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// add event listener to the form&lt;/span&gt;
&lt;span class="nx"&gt;form&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;submit&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// prevent default form submission behavior&lt;/span&gt;
  &lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;preventDefault&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

  &lt;span class="c1"&gt;// get the value of the input field and clear the field&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;todoText&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;input&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nx"&gt;input&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;""&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="c1"&gt;// add the todo item to the list and save it to the browser storage&lt;/span&gt;
  &lt;span class="nx"&gt;addTodoToList&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;todoText&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;saveTodoToStorage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;todoText&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="c1"&gt;// add a todo item to the list&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;addTodoToList&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;todoText&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;list&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;todo-list&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;item&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;createElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;li&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;innerText&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;todoText&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nx"&gt;list&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;appendChild&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// save a todo item to the browser storage&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;saveTodoToStorage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;todoText&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;chrome&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;storage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;sync&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;get&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;todos&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="kd"&gt;function&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;todos&lt;/span&gt; &lt;span class="o"&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;todos&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="p"&gt;[];&lt;/span&gt;
    &lt;span class="nx"&gt;todos&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;todoText&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;chrome&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;storage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;sync&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;set&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;todos&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;todos&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="c1"&gt;// load todos from the browser storage when the popup is opened&lt;/span&gt;
&lt;span class="nx"&gt;chrome&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;storage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;sync&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;get&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;todos&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="kd"&gt;function&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;todos&lt;/span&gt; &lt;span class="o"&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;todos&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="p"&gt;[];&lt;/span&gt;
  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;todo&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nx"&gt;todos&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;addTodoToList&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;todo&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;.&lt;br&gt;
A Simple and In-depth explanation of what the above code does:&lt;/p&gt;

&lt;p&gt;__&lt;br&gt;
The first three lines of the code define variables for the input, button, and list elements of the to-do list. These variables are used throughout the code to interact with these elements.&lt;br&gt;
__&lt;br&gt;
The next block of code adds an event listener to the button element that handles adding new to-do items to the list. When the button is clicked, the code gets the text from the input field, checks that it is not empty, creates a new list item element, sets its text content to the input text, adds it to the list, and clears the input field.&lt;br&gt;
__&lt;br&gt;
The next block of code loads any existing to-do items from storage when the popup is opened. This code uses the chrome.storage.sync API to get the "todos" key from storage, which contains an array of to-do items. If the "todos" key exists, the code creates a new list item element for each item in the array and adds it to the list.&lt;br&gt;
__&lt;br&gt;
The final block of code saves the current to-do items to storage when the popup is closed. This code uses the window.beforeunload event to detect when the popup is being closed. It then creates an array of the text content of each list item and saves it to the "todos" key in storage using the chrome.storage.sync API.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Overall, this code handles the creation, display, and storage of to-do items in the to-do list extension.&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 3:How to load your Chrome extension?:
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Open Google Chrome and type chrome://extensions in the address bar. This will take you to the Extensions page.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Click on the toggle switch in the upper right-hand corner to enable Developer Mode.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Click on the "Load unpacked" button in the upper left-hand corner. This will open a file dialog box.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Navigate to the directory where your extension is saved and select the folder.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Click "Select Folder". Your extension will now be loaded and ready to use.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Note: You will need to reload the extension every time you make changes to the code. To do this, go back to the Extensions page and click the "Reload" button next to your extension.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;.&lt;br&gt;
.&lt;/p&gt;

&lt;h2&gt;
  
  
  Before We End...
&lt;/h2&gt;

&lt;p&gt;That's all for now. I hope you've found this helpful&lt;/p&gt;

&lt;p&gt;Regards:&lt;br&gt;
Md Mafizuddin&lt;/p&gt;

&lt;p&gt;For any queries connect with me:&lt;br&gt;
LinkedIn: &lt;a href="https://www.linkedin.com/in/mafizonly/"&gt;@mafizonly&lt;/a&gt;&lt;br&gt;
Twitter: &lt;a href="https://twitter.com/whyknowmee"&gt;@whyknowmee&lt;/a&gt;&lt;br&gt;
Github: &lt;a href="https://github.com/pacehutt"&gt;@pacehut&lt;/a&gt;&lt;br&gt;
Instagram: &lt;a href="https://www.instagram.com/mafiz._/"&gt;@mafiz._&lt;/a&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>extension</category>
      <category>javascript</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Mastering React Hooks: Simplifying Your Components</title>
      <dc:creator>Md Mafiz Uddin</dc:creator>
      <pubDate>Sun, 29 Jan 2023 17:21:26 +0000</pubDate>
      <link>https://dev.to/pacehut/mastering-react-hooks-simplifying-your-components-1a55</link>
      <guid>https://dev.to/pacehut/mastering-react-hooks-simplifying-your-components-1a55</guid>
      <description>&lt;h2&gt;
  
  
  React Hooks
&lt;/h2&gt;

&lt;p&gt;React Hooks are a way to add state and other React features to functional components. They were introduced in React 16.8 and are a simpler way to manage state and component lifecycle methods in your components, compared to class components.&lt;/p&gt;

&lt;h3&gt;
  
  
  Popular React Hooks
&lt;/h3&gt;

&lt;p&gt;Here are some popular React Hooks that we will be discussing in this blog:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;- useState&lt;/li&gt;
&lt;li&gt;- useEffect&lt;/li&gt;
&lt;li&gt;- useContext&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let's dive into each of these hooks and understand what they do, and how to use them in our components.&lt;/p&gt;

&lt;p&gt;.&lt;br&gt;
.&lt;br&gt;
.&lt;/p&gt;
&lt;h4&gt;
  
  
  useState
&lt;/h4&gt;

&lt;p&gt;The useState hook allows us to add state to our functional component. State is data that can change and affect our component's rendering. We use useState by passing in an initial value, and it returns an array with two items: the current state value and a function to update it.&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="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Example&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="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;You&lt;/span&gt; &lt;span class="nx"&gt;clicked&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="nx"&gt;times&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/p&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="nx"&gt;onClick&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;setCount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&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;Click&lt;/span&gt; &lt;span class="nx"&gt;me&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/button&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;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;br&gt;
.&lt;br&gt;
.&lt;/p&gt;
&lt;h4&gt;
  
  
  &lt;strong&gt;useEffect&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;The &lt;code&gt;useEffect&lt;/code&gt;hook lets us run some code after every render of our component, and it can also control how often that code is run. This hook is great for making API calls, handling component cleanup, and more. We use &lt;code&gt;useEffect&lt;/code&gt;by passing in a callback function that runs after every render of the component. We can also pass in a second argument, an array of values, to control how often the effect runs.&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="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;useEffect&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="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Example&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="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="nf"&gt;useEffect&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="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;title&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;`You clicked &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; times`&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;count&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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;You&lt;/span&gt; &lt;span class="nx"&gt;clicked&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="nx"&gt;times&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/p&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="nx"&gt;onClick&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;setCount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&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;Click&lt;/span&gt; &lt;span class="nx"&gt;me&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/button&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;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;br&gt;
.&lt;br&gt;
.&lt;/p&gt;
&lt;h4&gt;
  
  
  &lt;strong&gt;useContext&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;The &lt;code&gt;useContext&lt;/code&gt;hook allows us to easily access and use data from our React context in our functional component. This hook makes it easy to share data between components without passing props down the component tree. We use &lt;code&gt;useContext&lt;/code&gt;by passing in the context we want to access and it returns the current value of that context.&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="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useContext&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="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;MyContext&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createContext&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Example&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;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useContext&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;MyContext&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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;The&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="nx"&gt;context&lt;/span&gt; &lt;span class="nx"&gt;is&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/p&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;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;h3&gt;
  
  
  Important Points :
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;React Hooks allow you to add state and other React features to functional components.&lt;/li&gt;
&lt;li&gt;Popular Hooks include useState, useEffect, useContext, useReducer, useRef, and useMemo.&lt;/li&gt;
&lt;li&gt;Hooks allow you to reuse state logic across multiple components without having to write a class component.&lt;/li&gt;
&lt;li&gt;Hooks can make your code cleaner, more organized, and easier to maintain.&lt;/li&gt;
&lt;li&gt;You can use Hooks to manage complex state, perform data fetching, handle side-effects, and optimize performance.&lt;/li&gt;
&lt;li&gt;React Hooks are a recent addition to the React library, but they have quickly become a popular way to write modern React applications.
.
.
#### Now
React Hooks are a powerful tool for adding state and other React features to functional components. Popular Hooks like &lt;code&gt;useState&lt;/code&gt;, &lt;code&gt;useEffect&lt;/code&gt;, &lt;code&gt;useContext&lt;/code&gt;, &lt;code&gt;useReducer&lt;/code&gt;, &lt;code&gt;useRef&lt;/code&gt;, and &lt;code&gt;useMemo&lt;/code&gt;provide a more organized, maintainable, and reusable way of writing React applications. By allowing developers to manage complex state, perform data fetching, handle side-effects, and optimize performance, Hooks have quickly become a popular way to write modern React applications.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Before We End...
&lt;/h2&gt;

&lt;p&gt;That's all for now. I hope you've found this helpful&lt;/p&gt;

&lt;p&gt;Regards:&lt;br&gt;
Md Mafizuddin&lt;/p&gt;

&lt;p&gt;For any queries connect with me:&lt;br&gt;
LinkedIn: &lt;a href="https://www.linkedin.com/in/mafizonly/" rel="noopener noreferrer"&gt;@mafizonly&lt;/a&gt;&lt;br&gt;
Twitter: &lt;a href="https://twitter.com/whyknowmee" rel="noopener noreferrer"&gt;@whyknowmee&lt;/a&gt;&lt;br&gt;
Github: &lt;a href="https://github.com/pacehutt" rel="noopener noreferrer"&gt;@pacehut&lt;/a&gt;&lt;br&gt;
Instagram: &lt;a href="https://www.instagram.com/mafiz._/" rel="noopener noreferrer"&gt;@mafiz._&lt;/a&gt;&lt;/p&gt;

</description>
      <category>productivity</category>
      <category>performance</category>
    </item>
    <item>
      <title>JavaScript Callbacks and Promises (Async Evolution)</title>
      <dc:creator>Md Mafiz Uddin</dc:creator>
      <pubDate>Sat, 28 Jan 2023 16:22:11 +0000</pubDate>
      <link>https://dev.to/pacehut/javascript-callbacks-and-promises-5g52</link>
      <guid>https://dev.to/pacehut/javascript-callbacks-and-promises-5g52</guid>
      <description>&lt;h2&gt;
  
  
  JavaScript Callbacks (why fear?)
&lt;/h2&gt;

&lt;p&gt;Every JavaScript beginner will face this question at least once: &lt;strong&gt;"What is a callback function?"&lt;/strong&gt;&lt;br&gt;
Well, we can find the answer in the word &lt;strong&gt;callback&lt;/strong&gt; itself. It's all about notifying the caller after the successful completion or failure of a task.&lt;/p&gt;
&lt;h2&gt;
  
  
  # First, what is a function?
&lt;/h2&gt;

&lt;p&gt;A function in JavaScript is a set of statements that performs a task. This set of statements can exist without a function, but having them in a function helps us reuse the task in multiple places.&lt;/p&gt;

&lt;p&gt;Suppose there is an example of a function that doubles a value if the value is an even number. We pass a number as an argument to the function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;doubleEven&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="p"&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="nx"&gt;n&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;2&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;n&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 you can use this function in as many places as you need to:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;doubleEven&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Output, 20&lt;/span&gt;
&lt;span class="nx"&gt;doubleEven&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="c1"&gt;// Output, 5&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But at any level of your journey to learn JS, you must be knowing that "&lt;strong&gt;&lt;em&gt;You can pass a function as an argument to another function"&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--UtVBe8hP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.ytimg.com/vi/TPEvfnXNO0E/maxresdefault.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--UtVBe8hP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.ytimg.com/vi/TPEvfnXNO0E/maxresdefault.jpg" alt='Les fonctions "Callback" en JavaScript 📞 - YouTube' width="880" height="495"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  # Callbacks
&lt;/h2&gt;

&lt;p&gt;A callback function is a function that is passed as an argument to another function, to be “called back” at a later time.&lt;/p&gt;

&lt;p&gt;But Why??&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Callbacks make sure that a function is not going to run before a task is completed but will run right after the task has completed&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;It helps us develop asynchronous JavaScript code and keeps us safe from problems and errors.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  # How to create a Callback?
&lt;/h2&gt;

&lt;p&gt;Let's assume there's a function &lt;strong&gt;foo()&lt;/strong&gt; which accepts a &lt;strong&gt;callback&lt;/strong&gt; and calls it wherever it wants in it's body.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
    &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;bar&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="nx"&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;foo is executing&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
          &lt;span class="nx"&gt;bar&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="nx"&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;foo called bar which is a callback&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;Now, How to call foo() ??&lt;/p&gt;

&lt;p&gt;For this we can directly write a function at the arguments' place or first we can create a function separately and then pass it as callback.&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="cm"&gt;/**
 Invoke foo by passing a function as an argument.
 */&lt;/span&gt;
   &lt;span class="nx"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&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="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;bar is called&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;Notice that we have passed the entire function definition as an argument to &lt;code&gt;foo&lt;/code&gt;. The passed function doesn't have a name. It is called an &lt;code&gt;anonymous function&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;We can also pass Arrow Functions as callbacks. &lt;br&gt;
Arrow functions are nothing but a different way of writing functions in JavaScript&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
 &lt;span class="nx"&gt;foo&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="nx"&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;callback function is called&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  # Built-In Methods of JavaScript:
&lt;/h2&gt;

&lt;p&gt;JavaScript provides built-in methods which accepts callbacks.&lt;br&gt;
e,g. &lt;strong&gt;&lt;code&gt;setTimeout()&lt;/code&gt;&lt;/strong&gt; , &lt;strong&gt;&lt;code&gt;setInterval()&lt;/code&gt;&lt;/strong&gt;......  &lt;/p&gt;

&lt;p&gt;Let's see,&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;message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&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="nx"&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;This message is shown after 3 seconds&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
     &lt;span class="p"&gt;}&lt;/span&gt;

     &lt;span class="nx"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
     &lt;span class="c1"&gt;//after 3 seconds it will log the statement provided&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;So, How this is working we will dive into it later.&lt;/p&gt;

&lt;h2&gt;
  
  
  # Promises in JS
&lt;/h2&gt;

&lt;p&gt;The constructor syntax for a promise object is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt; &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;promise&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&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="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// executor (the producing code)&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The function passed to  &lt;code&gt;new Promise&lt;/code&gt;  is called the  &lt;strong&gt;&lt;em&gt;executor&lt;/em&gt;&lt;/strong&gt;. When  &lt;code&gt;new Promise&lt;/code&gt;  is created, the executor runs automatically.&lt;/p&gt;

&lt;p&gt;Its arguments  &lt;code&gt;resolve&lt;/code&gt;  and  &lt;code&gt;reject&lt;/code&gt;  are callbacks provided by JavaScript itself. Our code is only inside the executor.&lt;/p&gt;

&lt;p&gt;When the executor obtains the result, be it soon or late, doesn’t matter, it should call one of these callbacks:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;code&gt;resolve(value)&lt;/code&gt;  — if the job is finished successfully, with result  &lt;code&gt;value&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;  &lt;code&gt;reject(error)&lt;/code&gt;  — if an error has occurred,  &lt;code&gt;error&lt;/code&gt;  is the error object.&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;So to summarize: the executor runs automatically and attempts to perform a job. When it is finished with the attempt, it calls  &lt;code&gt;resolve&lt;/code&gt;  if it was successful or  &lt;code&gt;reject&lt;/code&gt;  if there was an error.&lt;/p&gt;

&lt;p&gt;The  &lt;code&gt;promise&lt;/code&gt;  object returned by the  &lt;code&gt;new Promise&lt;/code&gt;  constructor has these internal properties:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;code&gt;state&lt;/code&gt;  — initially  &lt;code&gt;"pending"&lt;/code&gt;, then changes to either  &lt;code&gt;"fulfilled"&lt;/code&gt;  when  &lt;code&gt;resolve&lt;/code&gt;  is called or  &lt;code&gt;"rejected"&lt;/code&gt;  when  &lt;code&gt;reject&lt;/code&gt;  is called.&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;result&lt;/code&gt;  — initially  &lt;code&gt;undefined&lt;/code&gt;, then changes to  &lt;code&gt;value&lt;/code&gt;  when  &lt;code&gt;resolve(value)&lt;/code&gt;  is called or  &lt;code&gt;error&lt;/code&gt;  when  &lt;code&gt;reject(error)&lt;/code&gt;  is called.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--5Uo5dHBO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.pinimg.com/564x/1e/af/f2/1eaff2dcf56a40be1f21ae0e26900bc5.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--5Uo5dHBO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.pinimg.com/564x/1e/af/f2/1eaff2dcf56a40be1f21ae0e26900bc5.jpg" alt="" width="562" height="288"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here’s an example of a promise constructor and a simple executor function with “producing code” that takes time (via  &lt;code&gt;setTimeout&lt;/code&gt;):&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="s2"&gt;`      let promise = new Promise(function(resolve, reject) {
        // the function is executed automatically 
       //when the promise is constructed

        // after 1 second signal that the 
        // job is done with the result "done"
         setTimeout(() =&amp;gt; _resolve__("done")_, 1000);
      });
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can see two things by running the code above:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;The executor is called automatically and immediately (by  &lt;code&gt;new Promise&lt;/code&gt;).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;The executor receives two arguments:  &lt;code&gt;resolve&lt;/code&gt;  and  &lt;code&gt;reject&lt;/code&gt;. These functions are pre-defined by the JavaScript engine, so we don’t need to create them. We should only call one of them when ready.&lt;/p&gt;

&lt;p&gt;After one second of “processing”, the executor calls  &lt;code&gt;resolve("done")&lt;/code&gt;  to produce the result. This changes the state of the  &lt;code&gt;promise&lt;/code&gt;  object&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;That was an example of a successful job completion, a “fulfilled promise”.&lt;/p&gt;

&lt;p&gt;And now an example of the executor rejecting the promise with an error:&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="s2"&gt;`let promise = new Promise(function(resolve, reject) {
  // after 1 second signal that the job is finished with an error
  setTimeout(() =&amp;gt; _reject__(new Error("Whoops!"))_, 1000);
});
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;&lt;strong&gt;To summarize, the executor should perform a job (usually something that takes time) and then call  &lt;code&gt;resolve&lt;/code&gt;  or  &lt;code&gt;reject&lt;/code&gt;  to change the state of the corresponding promise object.&lt;br&gt;
A promise that is either resolved or rejected is called “settled”, as opposed to an initially “pending” promise.&lt;/strong&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  # Consumers: then, catch
&lt;/h2&gt;

&lt;p&gt;A Promise object serves as a link between the executor (the “producing code”) and the consuming functions, which will receive the result or error. Consuming functions can be registered (subscribed) using the methods  &lt;code&gt;.then&lt;/code&gt;  and  &lt;code&gt;.catch&lt;/code&gt;.&lt;/p&gt;
&lt;h3&gt;
  
  
  .then ( )
&lt;/h3&gt;

&lt;p&gt;The most important, fundamental one is  &lt;code&gt;.then&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The syntax is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;  
&lt;span class="kd"&gt;function&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;_&lt;/span&gt;&lt;span class="cm"&gt;/* handle a successful result */&lt;/span&gt;&lt;span class="nx"&gt;_&lt;/span&gt;  &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="kd"&gt;function&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;_&lt;/span&gt;&lt;span class="cm"&gt;/* handle an error */&lt;/span&gt;&lt;span class="nx"&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;h3&gt;
  
  
  .catch ( )
&lt;/h3&gt;

&lt;p&gt;The other method use to capture the errors of the promise object we use &lt;code&gt;.catch()&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;   &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;promise&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;  &lt;span class="k"&gt;new&lt;/span&gt;  &lt;span class="nb"&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="nx"&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;reject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt;  &lt;span class="nb"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Whoops!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)),&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
   &lt;span class="c1"&gt;// shows "Error: Whoops!" after 1 second_&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  # In Summary
&lt;/h2&gt;

&lt;p&gt;To Summarize:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  A JavaScript function can accept another function as an argument.&lt;/li&gt;
&lt;li&gt;  Passing the function as an argument is a powerful programming concept that can be used to notify a caller that something happened. It is also known as the callback function.&lt;/li&gt;
&lt;li&gt;  You can use callback functions to notify the caller depending on a use case. Callbacks are also used to carry out certain tasks depending on the state (pass, fail) of other tasks.&lt;/li&gt;
&lt;li&gt;  But be careful – nesting too many callback functions may not be a great idea and may create  &lt;code&gt;Callback Hell&lt;/code&gt;. We will dive into it sometime later..
##&lt;/li&gt;
&lt;li&gt; &lt;code&gt;Promise&lt;/code&gt;  is an important building block for the asynchronous concept in JavaScript.&lt;/li&gt;
&lt;li&gt;  A  &lt;code&gt;promise&lt;/code&gt;  object has two internal properties, state and result. These properties are not code-accessible.&lt;/li&gt;
&lt;li&gt;  The consumer of a promise can use the  &lt;code&gt;.then()&lt;/code&gt;,  &lt;code&gt;.catch()&lt;/code&gt;, and  &lt;code&gt;.finally()&lt;/code&gt;  methods to handle promises.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Before We End...
&lt;/h2&gt;

&lt;p&gt;That's all for now. I hope you've found this helpful&lt;/p&gt;

&lt;p&gt;Regards:&lt;br&gt;
Md Mafizuddin&lt;/p&gt;

&lt;p&gt;For any queries connect with me:&lt;br&gt;
LinkedIn: &lt;a href="https://www.linkedin.com/in/mafizonly/"&gt;@mafizonly&lt;/a&gt;&lt;br&gt;
Twitter: &lt;a href="https://twitter.com/whyknowmee"&gt;@whyknowmee&lt;/a&gt;&lt;br&gt;
Github: &lt;a href="https://github.com/pacehutt"&gt;@pacehut&lt;/a&gt;&lt;br&gt;
Instagram: &lt;a href="https://www.instagram.com/mafiz._/"&gt;@mafiz._&lt;/a&gt;&lt;/p&gt;

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