<?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: Nikhil Upadhyay</title>
    <description>The latest articles on DEV Community by Nikhil Upadhyay (@jerrycode06).</description>
    <link>https://dev.to/jerrycode06</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%2F393181%2F70767937-a9be-4715-8e62-c6f16a4e4f09.jpeg</url>
      <title>DEV Community: Nikhil Upadhyay</title>
      <link>https://dev.to/jerrycode06</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jerrycode06"/>
    <language>en</language>
    <item>
      <title>Using Web Workers for Parallel Processing in JavaScript</title>
      <dc:creator>Nikhil Upadhyay</dc:creator>
      <pubDate>Sun, 11 Aug 2024 21:39:55 +0000</pubDate>
      <link>https://dev.to/jerrycode06/using-web-workers-for-parallel-processing-in-javascript-3nhd</link>
      <guid>https://dev.to/jerrycode06/using-web-workers-for-parallel-processing-in-javascript-3nhd</guid>
      <description>&lt;p&gt;Ever felt your website slow down to a crawl when you're performing a heavy task like processing a large dataset or rendering complex graphics? It's a frustrating experience for both you and your users. Fear not, for there's a superhero in the world of JavaScript that can rescue your web app from this sluggishness: Web Workers. &lt;/p&gt;

&lt;p&gt;Think of Web Workers as your trusty sidekicks, handling the heavy lifting while your main script focuses on keeping the user interface smooth and responsive. They're like having a second brain for your JavaScript application, allowing you to perform tasks in the background without freezing the main thread.&lt;/p&gt;

&lt;h2&gt;
  
  
  Understanding Web Workers
&lt;/h2&gt;

&lt;p&gt;Web Workers are essentially JavaScript files that run in a separate thread, independent of the main thread. This means they can perform tasks without blocking the UI. Communication between the main thread and the worker happens through a message-passing system. &lt;/p&gt;

&lt;p&gt;To create a Web Worker, you instantiate a &lt;code&gt;Worker&lt;/code&gt; object, passing the path to the worker script as an argument. The worker script can then perform its tasks and send messages back to the main thread using the &lt;code&gt;postMessage&lt;/code&gt; method. The main thread listens for messages using the &lt;code&gt;onmessage&lt;/code&gt; event.&lt;/p&gt;

&lt;h2&gt;
  
  
  Common Use Cases
&lt;/h2&gt;

&lt;p&gt;Web Workers shine in a variety of scenarios: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Data Processing:&lt;/strong&gt; Handling large datasets, performing complex calculations, or generating reports. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Image and Video Processing:&lt;/strong&gt; Applying filters, resizing images, or encoding videos. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Game Development:&lt;/strong&gt; Performing physics calculations, AI processing, or loading game assets. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Cryptocurrency Mining:&lt;/strong&gt; While not recommended for most users due to energy consumption, Web Workers can be used to perform cryptocurrency mining calculations. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Offline Support:&lt;/strong&gt; Service Workers, a specialized type of Web Worker, can cache static assets and provide offline functionality.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Creating and Using Web Workers
&lt;/h2&gt;

&lt;p&gt;Let's dive into a simple example to see Web Workers in action. &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;// main.js&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;worker&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Worker&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;worker.js&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
&lt;span class="nx"&gt;worker&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;onmessage&lt;/span&gt; &lt;span class="o"&gt;=&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="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Message from worker:&lt;/span&gt;&lt;span class="dl"&gt;'&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="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
&lt;span class="p"&gt;};&lt;/span&gt; 
&lt;span class="nx"&gt;worker&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;postMessage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hello from main thread&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 


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

&lt;/div&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="c1"&gt;// worker.js&lt;/span&gt;

&lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;onmessage&lt;/span&gt; &lt;span class="o"&gt;=&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="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Message from main thread:&lt;/span&gt;&lt;span class="dl"&gt;'&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="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
  &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;postMessage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hello from worker&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;h3&gt;
  
  
  Let's break down the code -
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;main.js:&lt;/strong&gt; &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;code&gt;const worker = new Worker('worker.js');:&lt;/code&gt;&lt;/strong&gt; This line creates a new Web Worker instance named worker. The argument 'worker.js' specifies the path to the JavaScript file that will run in the worker thread. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;code&gt;worker.onmessage = (event) =&amp;gt; { ... }:&lt;/code&gt;&lt;/strong&gt; This line sets up an event listener on the worker object. Whenever the worker sends a message to the main thread, the code inside the event handler will be executed. The event.data property contains the data sent by the worker. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;code&gt;worker.postMessage('Hello from main thread');:&lt;/code&gt;&lt;/strong&gt; This line sends a message to the worker thread. The message content is 'Hello from main thread'. &lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;worker.js:&lt;/strong&gt; &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;code&gt;self.onmessage = (event) =&amp;gt; { ... }:&lt;/code&gt;&lt;/strong&gt; Similar to the main thread, this line sets up an event listener to receive messages from the main thread. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;code&gt;console.log('Message from main thread:', event.data);:&lt;/code&gt;&lt;/strong&gt; This line logs the received message to the console for debugging purposes. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;code&gt;self.postMessage('Hello from worker');:&lt;/code&gt;&lt;/strong&gt; This line sends a message back to the main thread with the content 'Hello from worker'. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The &lt;code&gt;self&lt;/code&gt; keyword in the worker script refers to the global object of the worker context. &lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media.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%2Fkxg771yos54kfmnao0by.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fkxg771yos54kfmnao0by.png" alt="Web Worker Interaction with web page"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Service Workers: Use case on Offline Support
&lt;/h2&gt;

&lt;p&gt;Service Workers are a special type of Web Worker that operate in the background, intercepting network requests and providing offline capabilities. They're crucial for building progressive web apps (PWAs). &lt;/p&gt;

&lt;p&gt;To achieve offline functionality, Progressive Web Apps (PWAs) rely heavily on Service Workers. These are JavaScript files that run in the background, separate from your main application script. They act as a proxy between your website and the network, allowing you to intercept and modify network requests.&lt;/p&gt;
&lt;h3&gt;
  
  
  Caching Static Assets
&lt;/h3&gt;

&lt;p&gt;The core idea behind offline support is to cache essential static assets, such as HTML, CSS, JavaScript, and images, during the initial load. When the user goes offline, these cached resources can be served, providing a seamless user experience. &lt;/p&gt;

&lt;p&gt;Here's a breakdown of the process: &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Register the Service Worker:&lt;/strong&gt; &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create a service-worker.js file in your project's root directory. &lt;/li&gt;
&lt;li&gt;Register it in your main JavaScript file: &lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="c1"&gt;// main.js&lt;/span&gt;
&lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;serviceWorker&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nb"&gt;navigator&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
  &lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;load&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="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
    &lt;span class="nb"&gt;navigator&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;serviceWorker&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;register&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;service-worker.js&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
      &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;registration&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
        &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Service Worker registered successfully:&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;registration&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
      &lt;span class="p"&gt;})&lt;/span&gt; 
      &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
        &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Service Worker registration failed:&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
      &lt;span class="p"&gt;});&lt;/span&gt; 
  &lt;span class="p"&gt;});&lt;/span&gt; 
&lt;span class="p"&gt;}&lt;/span&gt; 


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

&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;if ('serviceWorker' in navigator):&lt;/code&gt; This checks if the browser supports Service Workers. &lt;/li&gt;
&lt;li&gt;
&lt;code&gt;window.addEventListener('load', () =&amp;gt; { ... }):&lt;/code&gt; This ensures the code runs after the page has fully loaded. &lt;/li&gt;
&lt;li&gt;
&lt;code&gt;navigator.serviceWorker.register('service-worker.js'):&lt;/code&gt; This registers the service-worker.js file. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Cache Assets:&lt;/strong&gt;&lt;br&gt;
   Inside your &lt;code&gt;service-worker.js&lt;/code&gt; file, use the &lt;code&gt;CacheStorage&lt;/code&gt; API to &lt;br&gt;
   cache static assets.&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;// service-worker.js&lt;/span&gt;
&lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;install&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;event&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;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;waitUntil&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; 
    &lt;span class="nx"&gt;caches&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;static-cache-v1&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
      &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;cache&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;cache&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addAll&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt; 
          &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
          &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/index.html&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
          &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/styles.css&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
          &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/main.js&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 other static assets here &lt;/span&gt;
        &lt;span class="p"&gt;]);&lt;/span&gt; 
      &lt;span class="p"&gt;})&lt;/span&gt; 
  &lt;span class="p"&gt;);&lt;/span&gt; 
&lt;span class="p"&gt;});&lt;/span&gt; 


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

&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;self.addEventListener('install', (event) =&amp;gt; { ... })&lt;/code&gt;: This listens for the install event, which occurs when the Service Worker is first installed. &lt;/li&gt;
&lt;li&gt;
&lt;code&gt;event.waitUntil()&lt;/code&gt;: Ensures the installation process is complete before proceeding. &lt;/li&gt;
&lt;li&gt;
&lt;code&gt;caches.open('static-cache-v1')&lt;/code&gt;: Opens a cache named 'static-cache-v1'. &lt;/li&gt;
&lt;li&gt;
&lt;code&gt;cache.addAll([...])&lt;/code&gt;: Adds a list of assets to the cache. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Intercept Network Requests:&lt;/strong&gt;&lt;br&gt;
  Use the &lt;code&gt;fetch&lt;/code&gt; event to intercept network requests. If the requested &lt;br&gt;
  resource is in the cache, serve it from there; otherwise, fetch it from &lt;br&gt;
  the network.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;fetch&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;event&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;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;respondWith&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; 
    &lt;span class="nx"&gt;caches&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;match&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="nx"&gt;request&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
      &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;request&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
      &lt;span class="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;ul&gt;
&lt;li&gt;
&lt;code&gt;self.addEventListener('fetch', (event) =&amp;gt; { ... })&lt;/code&gt;: Listens for the fetch event, which occurs whenever a resource is requested. &lt;/li&gt;
&lt;li&gt;
&lt;code&gt;event.respondWith()&lt;/code&gt;: Defines how to respond to the request. &lt;/li&gt;
&lt;li&gt;
&lt;code&gt;caches.match(event.request)&lt;/code&gt;: Checks if the requested resource is in the cache. &lt;/li&gt;
&lt;li&gt;
&lt;code&gt;return response || fetch(event.request)&lt;/code&gt;: If the resource is in the cache, returns it; otherwise, fetches it from the network. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;By following these steps and considering the additional factors, you can effectively implement offline support in your PWA, providing a robust and reliable user experience, even when there's no internet connection. &lt;/p&gt;

&lt;p&gt;You can explore other use cases of service workers are – Push Notifications and Background Synchronization which are commonly used in making PWA apps.&lt;/p&gt;

&lt;h2&gt;
  
  
  Challenges and Considerations
&lt;/h2&gt;

&lt;p&gt;While Web Workers are powerful, they're not without their challenges: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Communication Overhead:&lt;/strong&gt; Sending data between the main thread and the worker can introduce overhead. &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Debugging Complexity:&lt;/strong&gt; Debugging Web Workers can be more challenging than debugging regular JavaScript code. &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Browser Compatibility:&lt;/strong&gt; Ensure compatibility across different browsers. &lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Web Workers are a game-changer for JavaScript developers, enabling you to create faster, more responsive, and offline-capable web applications. By understanding their core concepts and use cases, you can harness their power to build exceptional user experiences. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Remember:&lt;/strong&gt; While Web Workers are a valuable tool, it's essential to use them judiciously. Overusing them can lead to performance issues. Always profile your application to identify bottlenecks and determine where Web Workers can provide the most significant benefits.  &lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
    <item>
      <title>Frontend Optimization Techniques with Web Vitals: A Beginner's Guide</title>
      <dc:creator>Nikhil Upadhyay</dc:creator>
      <pubDate>Thu, 25 Jul 2024 14:14:12 +0000</pubDate>
      <link>https://dev.to/jerrycode06/frontend-optimization-techniques-with-web-vitals-a-beginners-guide-cj7</link>
      <guid>https://dev.to/jerrycode06/frontend-optimization-techniques-with-web-vitals-a-beginners-guide-cj7</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Hey there, fellow web developers! Ever clicked on a website and felt like you were waiting an eternity for it to load? Yeah, we've all been there. That's where frontend optimization comes in. It's like giving your website a turbo boost to make it lightning-fast. And to make sure your website is actually speedy, we have Web Vitals – a set of metrics that measure user experience. &lt;/p&gt;

&lt;p&gt;In this guide, we'll dive into the world of frontend optimization, break down those pesky Web Vitals, and give you practical tips to make your website a speed demon. Let's get started! &lt;/p&gt;

&lt;h2&gt;
  
  
  Understanding Web Vitals: The Heartbeat of User Experience
&lt;/h2&gt;

&lt;p&gt;Imagine Web Vitals as your website’s health check. These metrics tell you how happy your users are based on how quickly your site loads, how responsive it is, and how stable it is. There are three main Web Vitals: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Largest Contentful Paint (LCP):&lt;/strong&gt; This measures how long it takes for the largest content element on your page to load. Think of it as the moment when your website finally starts to look like something. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;First Contentful Paint (FCP):&lt;/strong&gt; This measures how long it takes for the very first content to render on the page. It's essentially the first impression your website makes. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;First Input Delay (FID):&lt;/strong&gt; This measures how long it takes for a user to interact with your website after they click or tap something. A high FID means your users are waiting around, twiddling their thumbs. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Cumulative Layout Shift (CLS):&lt;/strong&gt; This measures how much your website’s content moves around unexpectedly. Ever been about to click a button and it suddenly shifts, making you miss? That’s a layout shift. &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Optimize for Largest Contentful Paint (LCP)
&lt;/h2&gt;

&lt;p&gt;Let’s tackle LCP first. Remember, we want to get that biggest piece of content on the screen as quickly as possible. Here’s how: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Image Optimization:&lt;/strong&gt; Images can be huge file sizes. Compress them without sacrificing quality. Use formats like WebP if supported. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Critical Rendering Path:&lt;/strong&gt; Identify the resources your browser needs to render the initial view and optimize their delivery. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Server Response Time:&lt;/strong&gt; A slow server can kill your LCP. Make sure your server is up to snuff. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Avoid Render-Blocking Resources:&lt;/strong&gt; Scripts and stylesheets can block rendering. Defer non-critical resources or load them asynchronously. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Good score:&lt;/strong&gt; LCP should be less than 2.5 seconds. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Calculation:&lt;/strong&gt; LCP measures the time from when the page starts loading to when the largest content element is fully visible in the viewport. This could be an image, text block, or other content. &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.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%2Fn7jxbq68urc50svih8yw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fn7jxbq68urc50svih8yw.png" alt="LCP Scores" width="768" height="192"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Source - &lt;a href="https://web.dev/articles/lcp" rel="noopener noreferrer"&gt;https://web.dev/articles/lcp&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Optimize for First Contentful Paint (FCP)
&lt;/h2&gt;

&lt;p&gt;FCP is all about getting something on the screen as fast as possible. Here's how to improve it: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Prioritize above-the-fold content:&lt;/strong&gt; Make sure the most important content is loaded first. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Reduce server response time:&lt;/strong&gt; A fast server is crucial for a good FCP. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Optimize CSS delivery:&lt;/strong&gt; Deliver critical CSS as early as possible. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Minimize JavaScript execution time:&lt;/strong&gt; JavaScript can delay FCP. Minimize its impact. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Good score:&lt;/strong&gt; FCP should be less than 2 second. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Calculation:&lt;/strong&gt; FCP measures the time from when the page starts loading to when any part of the page's content is visible, even if it's just a small text element. &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Let's see an example of FCP and LCP timeline -&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2F9my3ijeyqnvi7w7fjz4n.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2F9my3ijeyqnvi7w7fjz4n.png" alt="FCP to LCP timeline" width="800" height="310"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Source - &lt;a href="https://web.dev/articles/lcp" rel="noopener noreferrer"&gt;https://web.dev/articles/lcp&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Improve First Input Delay (FID)
&lt;/h2&gt;

&lt;p&gt;FID is all about responsiveness. Nobody likes a slow website. Here’s how to boost your FID: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Minimize JavaScript Execution Time:&lt;/em&gt; JavaScript can be a performance hog. Break down large scripts, minimize their execution time, and avoid long-running tasks. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Reduce Main Thread Work:&lt;/em&gt; The main thread is busy handling user interactions. Offload heavy tasks to web workers if possible. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Leverage Browser Caching:&lt;/em&gt; Caching can significantly improve performance by storing resources locally. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Good score:&lt;/em&gt; FID should be less than 100 milliseconds. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Calculation:&lt;/em&gt; FID measures the time from when a user first interacts with a page (e.g., clicks a button) to when the browser is able to respond to that interaction.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.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%2Fh9h4t5cdtozbalphj2l1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fh9h4t5cdtozbalphj2l1.png" alt="FID Score" width="768" height="192"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Let's see the timeline of the typical webpage load and also you can see how FCP and FID are getting introduced in these timelines.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2Fyso4jejkyov67ckopt7a.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fyso4jejkyov67ckopt7a.png" alt="Webpoad load with FID and FCP" width="800" height="380"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Source - &lt;a href="https://web.dev/articles/fid" rel="noopener noreferrer"&gt;https://web.dev/articles/fid&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Reduce Cumulative Layout Shift (CLS)
&lt;/h2&gt;

&lt;p&gt;A stable website is a happy website. Let’s prevent those annoying layout shifts: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Use width and height attributes:&lt;/em&gt; Specify the dimensions of images and iframes to reserve their space upfront. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Avoid dynamic content placement:&lt;/em&gt; Don’t let elements change position unexpectedly. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Use font display:&lt;/em&gt; Control font loading to prevent layout shifts caused by font changes. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Good score:&lt;/em&gt; CLS should be less than 0.1. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Calculation:&lt;/em&gt; CLS measures the sum of all individual layout shift scores for each unexpected layout change. &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.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%2Fwfi8o60l1xgosckqumnk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fwfi8o60l1xgosckqumnk.png" alt="CLS Score" width="768" height="192"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Source - &lt;a href="https://web.dev/articles/cls" rel="noopener noreferrer"&gt;https://web.dev/articles/cls&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Optimize Your Code: The Nitty-Gritty
&lt;/h2&gt;

&lt;p&gt;Now, let's dive into some specific code optimization techniques: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Minification and Bundling:&lt;/em&gt; Remove unnecessary characters from your code and combine multiple files into one to reduce HTTP requests. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Code Splitting:&lt;/em&gt; Break down your code into smaller chunks to load only what's needed. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Leverage Browser Caching:&lt;/em&gt; Set appropriate cache headers to store static assets locally. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Reduce HTTP Requests:&lt;/em&gt; Combine files, use sprites, and optimize image formats. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Optimize CSS Delivery:&lt;/em&gt; Prioritize critical CSS and load the rest asynchronously. &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Measure and Iterate: The Ongoing Process
&lt;/h2&gt;

&lt;p&gt;Frontend optimization is an ongoing process. You need to measure your website's performance regularly and make improvements based on the data. Here are some tools to help you: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Google Lighthouse:&lt;/em&gt; This Chrome extension provides insights into your website's performance. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;WebPageTest:&lt;/em&gt; This tool offers detailed performance metrics and visualizations. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Chrome DevTools:&lt;/em&gt; Your browser's built-in developer tools can help you profile your code and identify performance bottlenecks. &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Frontend optimization is essential for creating a great user experience. By understanding Web Vitals and implementing the techniques we've discussed, you can significantly improve your website's performance. Remember, it's a journey, not a destination. Keep experimenting, measuring, and refining your optimization efforts.  &lt;/p&gt;

&lt;p&gt;If you want to read more and want to go deep into web vitals, please check out &lt;a href="https://web.dev/articles/vitals" rel="noopener noreferrer"&gt;web.dev articles on web vitals&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>programming</category>
      <category>learning</category>
    </item>
    <item>
      <title>Callback Hell and How to Rescue it ?</title>
      <dc:creator>Nikhil Upadhyay</dc:creator>
      <pubDate>Fri, 04 Jun 2021 14:24:22 +0000</pubDate>
      <link>https://dev.to/jerrycode06/callback-hell-and-how-to-rescue-it-ggj</link>
      <guid>https://dev.to/jerrycode06/callback-hell-and-how-to-rescue-it-ggj</guid>
      <description>&lt;p&gt;For understanding the concept of callbacks and callback hell, I think you should know about &lt;strong&gt;Synchronous&lt;/strong&gt; and &lt;strong&gt;Asynchronous&lt;/strong&gt; programming in JavaScript(or any other language). Let's see a quick view on these topics in context of JavaScript.&lt;/p&gt;

&lt;h2&gt;
  
  
  Synchronous Programming
&lt;/h2&gt;

&lt;p&gt;It is a way of programming in which you can perform only one task at a time and when one task is completed we move to another task. This is what we called &lt;strong&gt;Blocking Code&lt;/strong&gt; operation because you need to wait for a task to finish to move to the next one.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Program Starts&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;getSum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Program Ends&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above code snippet, you see code will execute line by line and when an operation on one line is finished then we move to the next line so this is just a simple example of the synchronous way of programming and we do this in our daily life of programming.&lt;/p&gt;

&lt;h2&gt;
  
  
  Asynchronous Programming
&lt;/h2&gt;

&lt;p&gt;Asynchronous programming allows you to perform that work without blocking the main process(or thread). It’s often related to parallelization, the art of performing independent tasks in parallel, that is achieved by using asynchronous programming.&lt;br&gt;
In asynchronous operation, you can move to another task before the previous one finishes, and this way you can deal with multiple requests simultaneously.&lt;br&gt;
In JavaScript, a good example of asynchronous programming is &lt;code&gt;setTimeout&lt;/code&gt; function, let's see a quick example -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Program Starts&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nf"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Reading an user from database...&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="mi"&gt;2000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Program Ends&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So, the output of this program will look like -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Program Starts
Program Ends
Reading an user from database...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Pretty cool, right? Our program didn't wait for &lt;code&gt;setTimeout&lt;/code&gt; to finish, just goes for the next line, then came back to the function and prints the output. This is what we called &lt;strong&gt;Non Blocking&lt;/strong&gt; code. You can read more about it &lt;a href="https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Asynchronous/Introducing" rel="noopener noreferrer"&gt;here&lt;/a&gt;. &lt;br&gt;
There are three design patterns in javascript to deal with asynchronous programming - &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Callbacks&lt;/li&gt;
&lt;li&gt;Promises&lt;/li&gt;
&lt;li&gt;async/await (just a syntactical sugar of promises)&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Callbacks
&lt;/h2&gt;

&lt;p&gt;Callbacks is a great way of handling asynchronous behavior in javascript. In JavaScript, everything behaves like an object so functions have the type of object and like any other object (strings, arrays, etc) you can pass functions as an argument to other functions and that's the idea of 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="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;getUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Reading an user from database...&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nf"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;githubUsername&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;jerrycode06&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;});&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="mi"&gt;2000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;getUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;User&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You see, we are passing function as an argument to &lt;code&gt;getUser&lt;/code&gt; function and it calls inside the &lt;code&gt;getUser&lt;/code&gt; function, output will look like -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Reading an user from database...
User {id: 1, githubUsername: 'jerrycode06'}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;p&gt;In above code snippet, we are getting user with github username now let's suppose you also want repositories for that username and also commits in the specific repository so what can we do with the callback approach -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;getUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;User&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nf"&gt;getRepositories&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;githubUsername&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;repos&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;repos&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nf"&gt;getCommits&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;repos&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="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;commits&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;commits&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="c1"&gt;// Callback Hell ("-_-)&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;You are seeing now a nesting of functions here and code also looks scary and this is what we called &lt;strong&gt;Callback Hell&lt;/strong&gt;. For a big application it creates more nesting.&lt;br&gt;
&lt;a href="https://media.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%2Fb8euo2n7twvgh3dbuatd.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fb8euo2n7twvgh3dbuatd.jpeg" alt="Callback Hell" width="721" height="420"&gt;&lt;/a&gt;&lt;br&gt;
To avoid this, we will see now &lt;strong&gt;Promises&lt;/strong&gt;.&lt;/p&gt;
&lt;h2&gt;
  
  
  Promises
&lt;/h2&gt;

&lt;p&gt;Promises are the alternative to callbacks for delivering the results of asynchronous computation. They require more effort from implementors of asynchronous functions, but provide several benefits for users of those functions. They are more readable as compared to callbacks and promises has many applications like &lt;code&gt;fetch&lt;/code&gt; in javascript , &lt;code&gt;mongoose&lt;/code&gt; operations and so on. Let's see how to implement promises with above example. Actually, promises have four states - &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;fulfilled - The action relating to the promise succeeded&lt;/li&gt;
&lt;li&gt;rejected - The action relating to the promise failed&lt;/li&gt;
&lt;li&gt;pending - Hasn't fulfilled or rejected yet&lt;/li&gt;
&lt;li&gt;settled - Has fulfilled or rejected
&lt;img src="https://media.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%2Fp60413nfwvkuezgtzzcx.png" alt="Promises" width="800" height="448"&gt;
First we have to create promises to understand this -
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;getUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Reading from a database....&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;githubUsername&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;jerrycode06&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="mi"&gt;2000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;getRepositories&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;username&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Extracting Repositories for &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;username&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;....`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;repo1&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;repo2&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;repo3&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
      &lt;span class="c1"&gt;// reject(new Error("Error occured in repositories"));&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="mi"&gt;2000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;getCommits&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;repo&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Extracting Commits for &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;repo&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;....&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;commits&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="mi"&gt;2000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;We created three functions, instead of passing callback function we are now returning a Promise which has two argument &lt;strong&gt;resolve&lt;/strong&gt; and &lt;strong&gt;reject&lt;/strong&gt;. If everything worked, call &lt;code&gt;resolve&lt;/code&gt; otherwise call &lt;code&gt;reject&lt;/code&gt;. Let's see how to use promises -&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;// Replace Callback with Promises to avoid callback hell&lt;/span&gt;
&lt;span class="nf"&gt;getUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;getRepositories&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;githubUsername&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;repos&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;getCommits&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;repos&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="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;commits&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Commits&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;commits&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Error: &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;More readable, Isn't it? Using arrow functions made this less complex than using simple functions. We have avoided nesting of functions and reduce the complexity of code (callback approach) and that's how promises work. You can deep-dive more about promises &lt;a href="https://web.dev/promises" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

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

&lt;p&gt;It is supposed to be the better way to write promises and it helps us keep our code simple and clean.&lt;br&gt;
&lt;a href="https://media.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%2F3s7u6tt9iqs9k80dtdkn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2F3s7u6tt9iqs9k80dtdkn.png" alt="Aync-Await" width="800" height="250"&gt;&lt;/a&gt;&lt;br&gt;
All you have to do is write the word &lt;code&gt;async&lt;/code&gt; before any regular function and it becomes a promise. In other words &lt;code&gt;async/await&lt;/code&gt; is a syntactical sugar of using promises it means if you want to avoid chaining of &lt;code&gt;then()&lt;/code&gt; methods in promises, so you can use the &lt;code&gt;async/await&lt;/code&gt; approach but internally it also uses the chaining. &lt;br&gt;
Let's see how to implement it with above example -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Async- await approach&lt;/span&gt;
&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;displayCommits&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;getUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&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;repos&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;getRepositories&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;githubUsername&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;commits&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;getCommits&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;repos&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;commits&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Error: &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;displayCommit&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, it is more readable than using promises above. Every time we use &lt;code&gt;await&lt;/code&gt;, we need to decorate this with a function with &lt;code&gt;async&lt;/code&gt;. Like promises, we don't have &lt;code&gt;catch()&lt;/code&gt; method here so that's why we are using &lt;code&gt;try-catch&lt;/code&gt; block for the error handling.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;In this article we've seen - &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Synchronous vs Asynchronous&lt;/li&gt;
&lt;li&gt;Callbacks and Callback Hell&lt;/li&gt;
&lt;li&gt;Avoid Callback hell with promises and async/await&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I personally like the async/await approach the most but sometimes we should the promises approach to deal with async behaviour. &lt;/p&gt;

&lt;p&gt;Thanks for reading this long post! I hope it helped you understand these topics  a little better. If you liked this post, then please do give me a few ❤️ and share it if you can. You are welcome to &lt;br&gt;
give any suggestions in comments and ask anything!&lt;/p&gt;

</description>
      <category>programming</category>
      <category>javascript</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Web Automation with Puppeteer - Node JS</title>
      <dc:creator>Nikhil Upadhyay</dc:creator>
      <pubDate>Fri, 25 Dec 2020 20:59:35 +0000</pubDate>
      <link>https://dev.to/jerrycode06/web-automation-with-puppeteer-node-js-4kg6</link>
      <guid>https://dev.to/jerrycode06/web-automation-with-puppeteer-node-js-4kg6</guid>
      <description>&lt;p&gt;I have a question in my mind right now, "Should I call this blog &lt;em&gt;Web Automation&lt;/em&gt; or &lt;em&gt;Web Scraping&lt;/em&gt; ?" Leave it for now.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&amp;lt; Hello World /&amp;gt;&lt;/strong&gt; I suppose you are here because "You search Puppeteer in dev.to" or "My Blog is trending right now" but I am here to tell you about Web Automation or Web Scraping. When I first read about Web Scraping, I had a question "In order to do these web automation or web scraping , Is Python only the language ?" The answer is no and I am here to tell you how to do this with Javascipt.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fdz5go698tg5ioaybwbvo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fdz5go698tg5ioaybwbvo.png" alt="Scraping Meme"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;h1&gt;
  
  
  Web Scraping
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;Web Scraping&lt;/strong&gt; is the process of extracting information from the internet, now the intention behind this can be research, education, business, analysis, and others. Basic web scraping script consists of a “crawler” that goes to the internet, surf around the web, and scrape information from given pages. We have gone over different web scraping tools by using programming languages and without programming like selenium, request, BeautifulSoup, MechanicalSoup, Parsehub, Diffbot, etc. It makes sense why everyone needs web scraping because it makes manual- data gathering processes very fast. And web scraping is the only solution when websites do not provide an API and data is needed. Collection of data from the web has various name like Web Scraping, Web Data Extraction &amp;amp; Web Harvesting. These days everything &amp;amp; everyone needs fuel to run. Data is the most precious fuel to run any organization. Finding the data is good; extracting it even better; doing it using automation is perfect. &lt;/p&gt;

&lt;h1&gt;
  
  
  Using Javascipt
&lt;/h1&gt;

&lt;p&gt;In this demonstration we are going to use Node JS and Puppeteer. &lt;a href="https://nodejs.org/en/" rel="noopener noreferrer"&gt;Node JS&lt;/a&gt; is a open source javascript run time environment built on &lt;a href="https://nodejs.dev/learn/the-v8-javascript-engine" rel="noopener noreferrer"&gt;Chrome's V8&lt;/a&gt; javascript engine written in C++ which enables javascript to run on your machine rather than your browser console. It is capable of reading or writing files on server and used in networking.&lt;/p&gt;

&lt;h3&gt;
  
  
  Puppeteer
&lt;/h3&gt;

&lt;p&gt;According to its official &lt;a href="https://pptr.dev/" rel="noopener noreferrer"&gt;documentation&lt;/a&gt; - &lt;br&gt;
&lt;em&gt;Puppeteer is a Node library which provides a high-level API to control Chrome or Chromium over the DevTools Protocol. Puppeteer runs headless by default, but can be configured to run full (non-headless) Chrome or Chromium.&lt;/em&gt;&lt;br&gt;
Let's understand this one-by-one - &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It is a node library &lt;/li&gt;
&lt;li&gt;Puppeteer provides us with a function to access Chrome or Chromium which in turn means we can automate anything we do on these browsers with it like emulating a key press, a click etc.&lt;/li&gt;
&lt;li&gt;By headless, it means that the whole operation on the browser by puppeteer can be done without ANY GUI (Graphical User Interface) .&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Using it we can Scare data from the internet, Create pdf from web pages, Take screenshots, Create automation testing and many more. &lt;br&gt;
Now I think you are understanding the hard text of documentation right now. If not, wait for it you will understand when we do coding part. &lt;/p&gt;

&lt;h3&gt;
  
  
  Prerequisites
&lt;/h3&gt;

&lt;p&gt;This tutorial is beginner friendly, no advanced knowledge of code is required. If you’re following along you’ll need NodeJS installed, basic knowledge of the command line, knowledge of JavaScript and knowledge of the DOM. If you don't have Node JS installed download it from &lt;a href="https://nodejs.org/en/download/" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Show Time
&lt;/h2&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Project Setup&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Make a folder with any name &lt;/li&gt;
&lt;li&gt;Open that folder in VS Code &lt;/li&gt;
&lt;li&gt;Open terminal in VS Code and type &lt;code&gt;npm init --yes&lt;/code&gt;. This will generate a &lt;code&gt;package.json&lt;/code&gt; for managing project dependencies like this - 
```
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;{&lt;br&gt;
  "name": "puppeteer-nodejs",&lt;br&gt;
  "version": "1.0.0",&lt;br&gt;
  "description": "",&lt;br&gt;
  "main": "index.js",&lt;br&gt;
  "scripts": {&lt;br&gt;
    "test": "echo \"Error: no test specified\" &amp;amp;&amp;amp; exit 1"&lt;br&gt;
  },&lt;br&gt;
  "keywords": [],&lt;br&gt;
  "author": "",&lt;br&gt;
  "license": "ISC"&lt;br&gt;
}&lt;/p&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;* Now install puppeteer by using command `npm install puppeteer` or you can also see their [documentation](https://github.com/puppeteer/puppeteer/blob/main/README.md) and read the installation. 
* After installing you can see the puppeteer in the dependencies in the `package.json` file like this - 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;{&lt;br&gt;
  "name": "puppeteer-nodejs",&lt;br&gt;
  "version": "1.0.0",&lt;br&gt;
  "description": "",&lt;br&gt;
  "main": "index.js",&lt;br&gt;
  "scripts": {&lt;br&gt;
    "test": "echo \"Error: no test specified\" &amp;amp;&amp;amp; exit 1"&lt;br&gt;
  },&lt;br&gt;
  "keywords": [],&lt;br&gt;
  "author": "",&lt;br&gt;
  "license": "ISC",&lt;br&gt;
  "dependencies": {&lt;br&gt;
    "puppeteer": "^5.5.0"&lt;br&gt;
  }&lt;br&gt;
}&lt;/p&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;* At last create you javascript file with any name, I personally prefer `index.js`.

###Quickstart
We will start with an easy example where we took screenshot of this [Corona Wikipedia Page](https://en.wikipedia.org/wiki/Coronavirus). In the `index.js` file write this code which you can see in the example code of documentation.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;const puppeteer = require("puppeteer");&lt;/p&gt;

&lt;p&gt;(async () =&amp;gt; {&lt;br&gt;
  const browser = await puppeteer.launch();&lt;br&gt;
  const page = await browser.newPage();&lt;br&gt;
  await page.goto("&lt;a href="https://en.wikipedia.org/wiki/Coronavirus%22" rel="noopener noreferrer"&gt;https://en.wikipedia.org/wiki/Coronavirus"&lt;/a&gt;);&lt;br&gt;
  await page.screenshot({ path: "corona-wiki.png" });&lt;br&gt;
  await browser.close();&lt;br&gt;
})();&lt;/p&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;So what this example does is it uses async [IIFE](https://developer.mozilla.org/en-US/docs/Glossary/IIFE) and wraps the whole script inside it . Now run this script by writing `node index.js` in terminal of VS Code and you will see something like this. 

![Code Image](https://dev-to-uploads.s3.amazonaws.com/i/nisr2dng14hqn4gcksu8.jpg) 

If everything went well You would see a new png file `corona-wiki.png` which contains the screenshot of our wikipedia page.

###Explanation
* `const puppeteer = require(‘puppeteer’);` is used to import puppeteer, it is going to be the first line of your scraper.
* `await puppeteer.launch();` is used to initiate a web browser or more specifically to create a browser instance you can open your browser in headless mode and non- headless mode using `{headless:false}` by default its true that means it will run browser processes in the background.
* We use await to wrap method calls in an async function, which we immediately invoke.
* `newPage()` method is used to get the page object. 
* `goto()` method to surf that URL and load it in the browser. 
* `screenshot()` takes a path argument and returns a screenshot of the webpage in 800×600 px form in the local directory.
* Once we are done with our script, we call `close()` method on the browser. 

###Scraping Data from Wikipedia
We will scrape some basic info from our Coronavirus Wikipedia page and output this in our console or you can put it in separate file(.txt, .dat, etc) like we did above. 
First of all let's go to our wikipedia page and scroll down a little you will see some headings so here we are - 

![Blog Image 3](https://dev-to-uploads.s3.amazonaws.com/i/fgcrdkt28xptx1rrivj3.png) 

Now we will collect these all heading from this page now right click and you will see **Inspect Element** click it this will open your **Inpector**. I am using Mozilla , you can Chrome also and the process is same because we just need some class-info from inspector. 

![Blog Image](https://dev-to-uploads.s3.amazonaws.com/i/p8dhqahbu9kyr33s5163.jpg)

You can see the Heading `&amp;lt;h2&amp;gt;` contains span of `class = "mw-headline"` and this class is common for all heading and it is our path to collect all headings. 

We add this function in our code - 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;const result = await page.evaluate(() =&amp;gt; {&lt;br&gt;
    let headingFromWeb = document.querySelectorAll(".mw-headline");&lt;br&gt;
    const headingList = [...headingFromWeb];&lt;br&gt;
    return headingList.map((h) =&amp;gt; h.innerText);&lt;br&gt;
  });&lt;/p&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;* We are using `page.evaluate()` function for this process and store it in a variable `result`.
* We are selecting that class with `document.querySelectorAll(".mw-headline");`
* Make it an array using the [spread operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax) and store in other variable `headingList`.
* [Map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map) that array and return the inner text part which contains our Headings. 

Our final code will look like this - 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;const puppeteer = require("puppeteer");&lt;/p&gt;

&lt;p&gt;(async () =&amp;gt; {&lt;br&gt;
  const browser = await puppeteer.launch();&lt;br&gt;
  const page = await browser.newPage();&lt;br&gt;
  await page.goto("&lt;a href="https://en.wikipedia.org/wiki/Coronavirus%22" rel="noopener noreferrer"&gt;https://en.wikipedia.org/wiki/Coronavirus"&lt;/a&gt;);&lt;br&gt;
  const result = await page.evaluate(() =&amp;gt; {&lt;br&gt;
    let headingFromWeb = document.querySelectorAll(".mw-headline");&lt;br&gt;
    const headingList = [...headingFromWeb];&lt;br&gt;
    return headingList.map((h) =&amp;gt; h.innerText);&lt;br&gt;
  });&lt;/p&gt;

&lt;p&gt;console.log(result);&lt;br&gt;
  await browser.close();&lt;br&gt;
})();&lt;/p&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;We are showing our result in terminal that's why we are using `console.log(result);`. If you have done right till now then you will see output like this - 

![Blog Image](https://dev-to-uploads.s3.amazonaws.com/i/3mpoco841yf5bc79t43z.jpg)

If you are seeing something like this then pat on your back you have done a great job, you did scraping from a famous site. If you don't understand some keywords first time, Don't worry I have attached the links where you can find great resource and read it on your own. 
There are many things to web scraping like going to different pages like IMDb, your college site, etc. Try out on your own read the documentation and you'll like puppeteer like me.

Thanks for reading this long post! I hope it helped you understand Web Scraping a little better. If you liked this post, then please do give me a few ❤️. You are welcome to comment and ask anything!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

</description>
      <category>webdev</category>
      <category>node</category>
      <category>help</category>
    </item>
    <item>
      <title>UI/UX - Design Thinking</title>
      <dc:creator>Nikhil Upadhyay</dc:creator>
      <pubDate>Thu, 12 Nov 2020 19:47:32 +0000</pubDate>
      <link>https://dev.to/jerrycode06/ui-ux-design-thinking-1ph4</link>
      <guid>https://dev.to/jerrycode06/ui-ux-design-thinking-1ph4</guid>
      <description>&lt;p&gt;Design Thinking is a human centered, iterative, creative, problem-solving process. It helps teams to manufacture innovation. It keeps people at the center of the process with the hope to create products that actually meet the needs and goal of the user.&lt;/p&gt;

&lt;p&gt;Design Thinking is not the sole property of UXers. It's widely used across industries for decades.&lt;/p&gt;

&lt;p&gt;As described by IDEO, one of the leaders in design thinking, "This approach, which is known as design thinking, brings together what is desirable from a human point of view with what is technologically feasible and economically viable. It also allows people who aren't trained as designers to use creative tools to address a vast range of challenges".&lt;/p&gt;

&lt;h1&gt;
  
  
  Design Thinking consist of five phases.
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://media.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%2Fw8ywtyz25fep5c0a5vzl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fw8ywtyz25fep5c0a5vzl.png" alt="Design Phases" width="800" height="268"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Empathize
&lt;/h2&gt;

&lt;p&gt;This is where you learn about user problems,needs and goals. You're looking to gain empathy for the user and the problem they're experiencing. Empathy is at the center of the design thinking and UX as a whole because it helps you look at the problem from user's perspective and not your own.&lt;/p&gt;

&lt;h2&gt;
  
  
  Define
&lt;/h2&gt;

&lt;p&gt;After learning all about the problem during the empathize phase, you analyze the data collected and define the problem. Here you create things like affinity maps and personas to guide you through the rest of the design process. The goal is to build a strong foundation for your ideas. &lt;/p&gt;

&lt;h2&gt;
  
  
  Ideate
&lt;/h2&gt;

&lt;p&gt;Now that the problem is defined, its time to try out a bunch of things. You want to generate as many ideas as possible. You’re looking to challenge the status quo and any of the assumptions about the problem you stated earlier in the process. You can’t know the best solution until you’ve considered all the options.&lt;/p&gt;

&lt;h2&gt;
  
  
  Prototype
&lt;/h2&gt;

&lt;p&gt;After ideating, it’s time to start experimenting with the different ideas you came up with during the previous phase and begin creating prototypes that you’ll test.&lt;/p&gt;

&lt;h2&gt;
  
  
  Test
&lt;/h2&gt;

&lt;p&gt;With your prototypes in hand, it's time to learn if your solutions hit the mark. This is where you'll learn usability tests with real users to see what they think. You’ll get valuable feedback that you’ll use to either return to previous phases of the process to iterate further, or to move on to a final stage of production. &lt;/p&gt;

&lt;h1&gt;
  
  
  Final Words
&lt;/h1&gt;

&lt;p&gt;It’s important to point out that these phases aren’t linear and it’s not a step-by-step process. It’s typical to jump back and forth between them and/or run phases in parallel.&lt;br&gt;
&lt;a href="https://media.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%2Fm6lsi95di4rbho6wiwm1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fm6lsi95di4rbho6wiwm1.png" alt="Non-Liner Design Phases" width="800" height="390"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Additionally, while there are a vast number of methods you can use during each phase, the 5 phases always remain the same. Think of the process as being dynamic. Depending on the context and the problem you’re solving, you apply the phase and methods that make the most sense.&lt;br&gt;
The Empathize and Define stages are the equivalent to Research &amp;amp; understanding. Ideate is the same as Information architecture &amp;amp; wireframing. Prototype and Test are the same as... you guessed it... the Prototyping &amp;amp; usability testing phase. &lt;/p&gt;

</description>
      <category>ux</category>
      <category>uiweekly</category>
      <category>webdev</category>
      <category>design</category>
    </item>
    <item>
      <title>My Graduation Project Experience</title>
      <dc:creator>Nikhil Upadhyay</dc:creator>
      <pubDate>Fri, 22 May 2020 21:25:14 +0000</pubDate>
      <link>https://dev.to/jerrycode06/my-graduation-project-experience-5hm2</link>
      <guid>https://dev.to/jerrycode06/my-graduation-project-experience-5hm2</guid>
      <description>&lt;p&gt;Hey, I am Nikhil Upadhyay and this is the first time I am writing a blog post on any platform so it may be a short one. I've completed my graduation with Physics as my major subject. I like physics very much but when it comes to deep theoretical physics it becomes vulnerable and I am saying this from my experience that's why I like experimental physics very much.&lt;br&gt;
So, This was the time of my end semester and there is rule to complete a major project under a supervisor before you graduate. So, my department allotted me a professor Dr. Raktim Abir (Core Area- Quantum Chromodynamics and Chaotic Systems). During my graduation I learned some programming Languages like C/C++ and some Python so when I first met my Professor he nicely talked to me and asked me a first question "Nikhil, this is your last semester so would you like to tell me how was your experience in this department or this course?" then this discussion goes on. As I told him about that I want to change my subject from physics to Computer Science, so he gave me a topic which is related to Computer Science and Physics "Cellular Automaton". Like you I also didn't hear that name before so I go to my friend Google and research on my own. During this project, I have to study "What is Cellular Automaton and how this work?" and also I have to make some algorithms for famous Cellular Automaton models like Game of Life and Abelian Sandpile's Model. I got to know a YouTube channel TheCodingTrain from Prof. Daniel Shiffman ,he is pretty smart and funny guy and I saw his tutorial then I got familiar and then it takes 3 months and finally I submitted my Project and gave the presentation. That's It!!&lt;br&gt;
Thank You     &lt;/p&gt;

</description>
      <category>productivity</category>
      <category>cpp</category>
      <category>computerscience</category>
      <category>challenge</category>
    </item>
  </channel>
</rss>
