<?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: Sudarsan</title>
    <description>The latest articles on DEV Community by Sudarsan (@tjsudarsan).</description>
    <link>https://dev.to/tjsudarsan</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%2F500286%2F304c14c2-d380-463d-bced-9860f2bff94e.jpeg</url>
      <title>DEV Community: Sudarsan</title>
      <link>https://dev.to/tjsudarsan</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/tjsudarsan"/>
    <language>en</language>
    <item>
      <title>Sequential vs Parallel Processing in JS</title>
      <dc:creator>Sudarsan</dc:creator>
      <pubDate>Sun, 08 Nov 2020 05:35:48 +0000</pubDate>
      <link>https://dev.to/tjsudarsan/sequential-vs-parallel-processing-in-js-4gn3</link>
      <guid>https://dev.to/tjsudarsan/sequential-vs-parallel-processing-in-js-4gn3</guid>
      <description>&lt;p&gt;Everyone knows that &lt;strong&gt;Sequential Processing&lt;/strong&gt; takes a lot of time when comparing to &lt;strong&gt;Parallel Processing&lt;/strong&gt;!&lt;/p&gt;

&lt;p&gt;Users expect apps to respond faster these days and don't want to wait too long to get something out of it. Whether it's a &lt;em&gt;Web or Mobile App&lt;/em&gt; it doesn't matter for them, they will close it and leave.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Even though your apps load faster under &lt;em&gt;3secs&lt;/em&gt;, some modules may take lots of time like &lt;em&gt;30secs or even 1min&lt;/em&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Some of the examples are:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Populating data which has many references to other data&lt;/li&gt;
&lt;li&gt;Visualizing data&lt;/li&gt;
&lt;li&gt;Too many API requests which independent to one another&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;And there are a lot many like this!&lt;/p&gt;

&lt;p&gt;Let's take &lt;strong&gt;filter&lt;/strong&gt; module in a &lt;em&gt;web or mobile app&lt;/em&gt; which requires different data for different constraints!&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%2Fccx107xg7pwgss6vlhuk.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%2Fccx107xg7pwgss6vlhuk.png" alt="Screenshot of a Filter in Flipkart"&gt;&lt;/a&gt;&lt;/p&gt;
Screenshot of a Filter in Flipkart



&lt;p&gt;As you can see the above filter has too many constraints to be fetched!&lt;/p&gt;

&lt;p&gt;Let's use this example to compare Sequential and Parallel processing!&lt;/p&gt;

&lt;h4&gt;
  
  
  Sequential data fetching
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// The function given below fetches data from the server one-by-one&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;fetchFilterData&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async &lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;brands&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;fetchBrands&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;sizes&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;fetchSizes&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;occasions&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;fetchOccasions&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;idealFor&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;fetchIdealFor&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;neckTypes&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;fetchNeckTypes&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;sleeveTypes&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;fetchSleeveTypes&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;patterns&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;fetchPatterns&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;color&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;fetchColors&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

    &lt;span class="c1"&gt;// Now, display list of fetched data in filters&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Something went wrong!&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="c1"&gt;// Push the error to your crash analytics to track the bug&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;The above code has 8 requests to display filter items as in the above image. It's &lt;code&gt;async&lt;/code&gt; &lt;code&gt;await&lt;/code&gt; code where each request will wait for the previous request to complete successfully.&lt;/p&gt;

&lt;p&gt;Let's do some Math with the above code to calculate the time taken to complete the process!&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%2F1sojqw7zexedd991nxu4.jpg" 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%2F1sojqw7zexedd991nxu4.jpg" alt="Sequential requests"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Let's assume that the user has slower internet speed!&lt;/strong&gt;&lt;br&gt;
Time taken for one API request = ~800ms&lt;br&gt;
Number of API requests = 8&lt;br&gt;
Total time to complete all requests one-by-one = 8 x 800ms = 6400ms = &lt;strong&gt;6.4secs&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So the user has to wait for &lt;em&gt;6.4secs&lt;/em&gt; to engage with filters, That's a lot of time, right? Also, this is only for 8 constrains in a filter. Imagine how much time it will take when there are so many constraints like 20nos. No one will wait for that much longer!&lt;/p&gt;

&lt;p&gt;To solve this, we need to use &lt;em&gt;Parallel processing&lt;/em&gt; in &lt;em&gt;JavaScript&lt;/em&gt;!&lt;/p&gt;
&lt;h4&gt;
  
  
  Parallel data fetching
&lt;/h4&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// lets consider the below function that fetches data from server&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;fetchFilterData&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async &lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;all&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;
  &lt;span class="nf"&gt;fetchBrands&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
  &lt;span class="nf"&gt;fetchSizes&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
  &lt;span class="nf"&gt;fetchOccasions&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
  &lt;span class="nf"&gt;fetchIdealFor&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
  &lt;span class="nf"&gt;fetchNeckTypes&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
  &lt;span class="nf"&gt;fetchSleeveTypes&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
  &lt;span class="nf"&gt;fetchPatterns&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
  &lt;span class="nf"&gt;fetchColors&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;renderFilter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async &lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
      &lt;span class="nx"&gt;brands&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="nx"&gt;sizes&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="nx"&gt;occasions&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="nx"&gt;idealFor&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="nx"&gt;neckTypes&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="nx"&gt;sleeveTypes&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="nx"&gt;patterns&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="nx"&gt;color&lt;/span&gt;
    &lt;span class="p"&gt;]&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;fetchFilterData&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

    &lt;span class="c1"&gt;// Now, display list of fetched data in filters&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Something went wrong!&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="c1"&gt;// Push the error to your crash analytics to track the bug&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;The above code has 8 requests to display filter items as in the above image.&lt;/p&gt;

&lt;p&gt;It's &lt;code&gt;Promise.all&lt;/code&gt; code where each request will be fired at the same time resulting in &lt;strong&gt;Array of Promises&lt;/strong&gt; as shown below,&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Promise.all([&amp;lt;Promise&amp;gt;, &amp;lt;Promise&amp;gt;, &amp;lt;Promise&amp;gt;, ..., &amp;lt;Promise&amp;gt;])&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now, &lt;code&gt;Promise.all()&lt;/code&gt; will be &lt;em&gt;resolved&lt;/em&gt; once &lt;strong&gt;every promise in the array gets resolved&lt;/strong&gt;!&lt;/p&gt;

&lt;p&gt;Let's do some Math with the above code to calculate the time taken to complete the process!&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%2F78k1qe7ig11drcykbnzp.jpg" 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%2F78k1qe7ig11drcykbnzp.jpg" alt="Parallel Requests"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Let's assume that the user has slower internet speed!&lt;/strong&gt;&lt;br&gt;
Time taken to complete one API request = ~800ms&lt;br&gt;
Total time to complete all requests in parallel = ~800ms = &lt;strong&gt;0.8secs or 1sec&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So the user doesn't want to wait too long as it takes only &lt;em&gt;~1.3secs&lt;/em&gt; to engage! This is because all the 8 requests are made in parallel!&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;This method can be applied in &lt;em&gt;serverside&lt;/em&gt; in handling &lt;strong&gt;API&lt;/strong&gt; request using &lt;strong&gt;Nodejs&lt;/strong&gt;. It can reduce the API response time significantly while querying multiple tables simultaneously from a database using &lt;code&gt;Promise.all()&lt;/code&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h4&gt;
  
  
  Also, this method can be used when promises are involved anywhere, provided that there is no dependency with each other. If they are depended go for sequential processing.
&lt;/h4&gt;

&lt;p&gt;Note: This is just an example and not the real code of filter in Flipkart. I designed in a way that most of the requests can be made parallel using this method.&lt;/p&gt;

&lt;p&gt;Read the following article to improve code readability while handling promises!&lt;/p&gt;


&lt;div class="ltag__link"&gt;
  &lt;a href="/tjsudarsan" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__pic"&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%2Fuser%2Fprofile_image%2F500286%2F304c14c2-d380-463d-bced-9860f2bff94e.jpeg" alt="tjsudarsan"&gt;
    &lt;/div&gt;
  &lt;/a&gt;
  &lt;a href="/tjsudarsan/handling-promises-traditional-way-vs-modern-way-3229" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__content"&gt;
      &lt;h2&gt;How I am Handling Promises - Traditional way vs Modern way&lt;/h2&gt;
      &lt;h3&gt;Sudarsan ・ Nov 1 '20&lt;/h3&gt;
      &lt;div class="ltag__link__taglist"&gt;
        &lt;span class="ltag__link__tag"&gt;#javascript&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#react&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#webdev&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#node&lt;/span&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/a&gt;
&lt;/div&gt;



</description>
      <category>javascript</category>
      <category>node</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Optional?.Chaining 🤩 - A great gift for Devs</title>
      <dc:creator>Sudarsan</dc:creator>
      <pubDate>Mon, 02 Nov 2020 11:41:14 +0000</pubDate>
      <link>https://dev.to/tjsudarsan/optional-chaining-a-great-gift-for-devs-gfb</link>
      <guid>https://dev.to/tjsudarsan/optional-chaining-a-great-gift-for-devs-gfb</guid>
      <description>&lt;p&gt;Traditional way of validating a condition which has nested objects&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;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
   &lt;span class="nx"&gt;person&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;person&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;education&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; 
   &lt;span class="nx"&gt;person&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;education&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;highSchool&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt;
   &lt;span class="nx"&gt;person&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;education&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;highSchool&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;mark&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt;
   &lt;span class="nx"&gt;person&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;education&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;highSchool&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;mark&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;maths&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;😭&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;Okay! why we need to right like this? Because, if we didn't validate each key for &lt;code&gt;undefined&lt;/code&gt; then there's a huge chance of errors and your program crashes! To avoid we need to validate every &lt;strong&gt;key's value&lt;/strong&gt; for &lt;code&gt;undefined&lt;/code&gt;!&lt;/p&gt;

&lt;p&gt;Otherwise, we may get one of the following errors&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;code&gt;Error: cannot read property education of undefined&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Error: cannot read property highSchool of undefined&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Error: cannot read property mark of undefined&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Error: cannot read property maths of undefined&lt;/code&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Also, these errors may occur not only for &lt;code&gt;undefined&lt;/code&gt;, but also for &lt;code&gt;null&lt;/code&gt; value too.&lt;/p&gt;

&lt;p&gt;This can be hard if you have nested object of depth more than 4 😭&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;How to get rid of this?&lt;/strong&gt;&lt;br&gt;
we can use Optional Chaining&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Modern way!&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;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;person&lt;/span&gt;&lt;span class="p"&gt;?.&lt;/span&gt;&lt;span class="nx"&gt;education&lt;/span&gt;&lt;span class="p"&gt;?.&lt;/span&gt;&lt;span class="nx"&gt;highSchool&lt;/span&gt;&lt;span class="p"&gt;?.&lt;/span&gt;&lt;span class="nx"&gt;mark&lt;/span&gt;&lt;span class="p"&gt;?.&lt;/span&gt;&lt;span class="nx"&gt;maths&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;Life saver! 🤩&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;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; Modern browsers supports &lt;em&gt;Optional Chaining&lt;/em&gt;, for &lt;strong&gt;nodejs&lt;/strong&gt; projects you should have &lt;em&gt;nodejs v14.x&lt;/em&gt; version installed.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Or (simple hack without Optional Chaining)😉&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;if&lt;/span&gt;&lt;span class="p"&gt;(((((&lt;/span&gt;&lt;span class="nx"&gt;person&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="p"&gt;{}).&lt;/span&gt;&lt;span class="nx"&gt;education&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="p"&gt;{}).&lt;/span&gt;&lt;span class="nx"&gt;highSchool&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="p"&gt;{}).&lt;/span&gt;&lt;span class="nx"&gt;mark&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="p"&gt;{}).&lt;/span&gt;&lt;span class="nx"&gt;maths&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;Looks weird! but easy!&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;That's it instead of coding person person person..... Blah blah 🤥&lt;/p&gt;

&lt;p&gt;You can code like this! 🌟&lt;/p&gt;

&lt;p&gt;Try this on your project and comment your thoughts!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>react</category>
      <category>node</category>
    </item>
    <item>
      <title>How I am Handling Promises - Traditional way vs Modern way</title>
      <dc:creator>Sudarsan</dc:creator>
      <pubDate>Sun, 01 Nov 2020 04:20:48 +0000</pubDate>
      <link>https://dev.to/tjsudarsan/handling-promises-traditional-way-vs-modern-way-3229</link>
      <guid>https://dev.to/tjsudarsan/handling-promises-traditional-way-vs-modern-way-3229</guid>
      <description>&lt;p&gt;Handling &lt;strong&gt;Promises&lt;/strong&gt; are very hard now a days, but there’s an easy way to deal it!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Traditional Way:&lt;/strong&gt; so many &lt;em&gt;then&lt;/em&gt; and &lt;em&gt;catch&lt;/em&gt; blocks 🥴&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%2F58fn2bchrcmu8jwjux8o.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%2F58fn2bchrcmu8jwjux8o.png" alt="old way of handling promises"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Modern Way:&lt;/strong&gt; With the help of &lt;em&gt;async&lt;/em&gt; and &lt;em&gt;await&lt;/em&gt; we can get rid of &lt;em&gt;then&lt;/em&gt; and &lt;em&gt;catch&lt;/em&gt; blocks and improves code readability! 🥳&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%2F7forxcbz57k457efysye.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%2F7forxcbz57k457efysye.png" alt="modern way of handling promises"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;&lt;em&gt;Comment your thoughts and share to your friends&lt;/em&gt;&lt;/p&gt;

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