<?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: Karan Kiri</title>
    <description>The latest articles on DEV Community by Karan Kiri (@karankiri).</description>
    <link>https://dev.to/karankiri</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.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F280992%2Fb3ac633c-1a69-437a-b53a-3e70337b8ca7.jpeg</url>
      <title>DEV Community: Karan Kiri</title>
      <link>https://dev.to/karankiri</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/karankiri"/>
    <language>en</language>
    <item>
      <title>Promise.all vs Promise.allSettled in Javascript</title>
      <dc:creator>Karan Kiri</dc:creator>
      <pubDate>Wed, 15 Mar 2023 14:19:10 +0000</pubDate>
      <link>https://dev.to/karankiri/promiseall-vs-promiseallsettled-in-javascript-41o9</link>
      <guid>https://dev.to/karankiri/promiseall-vs-promiseallsettled-in-javascript-41o9</guid>
      <description>&lt;p&gt;&lt;code&gt;Promise. all&lt;/code&gt; and &lt;code&gt;Promise.allSettled&lt;/code&gt; is used to iterate over an array of promises. However, both have a few subtle differences, let's understand that in this article. &lt;/p&gt;

&lt;h3 id="heading-promiseall-method"&gt;
&lt;code&gt;Promise.all&lt;/code&gt; method&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;Promise. all&lt;/code&gt; iterates over an array of promises and resolve to the array of values returned by each promise.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&lt;span class="hljs-keyword"&gt;const&lt;/span&gt; tasks = [
    &lt;span class="hljs-keyword"&gt;new&lt;/span&gt; &lt;span class="hljs-built_in"&gt;Promise&lt;/span&gt;(&lt;span class="hljs-function"&gt;(&lt;span&gt;resolve, reject&lt;/span&gt;)=&amp;gt;&lt;/span&gt; {
        &lt;span class="hljs-built_in"&gt;setTimeout&lt;/span&gt;(resolve, &lt;span class="hljs-number"&gt;1000&lt;/span&gt;, &lt;span class="hljs-string"&gt;"task 1"&lt;/span&gt;)
    }),
    &lt;span class="hljs-keyword"&gt;new&lt;/span&gt; &lt;span class="hljs-built_in"&gt;Promise&lt;/span&gt;(&lt;span class="hljs-function"&gt;(&lt;span&gt;resolve, reject&lt;/span&gt;)=&amp;gt;&lt;/span&gt; {
        &lt;span class="hljs-built_in"&gt;setTimeout&lt;/span&gt;(resolve, &lt;span class="hljs-number"&gt;2000&lt;/span&gt;, &lt;span class="hljs-string"&gt;"task 2"&lt;/span&gt;)
    }),
    &lt;span class="hljs-keyword"&gt;new&lt;/span&gt; &lt;span class="hljs-built_in"&gt;Promise&lt;/span&gt;(&lt;span class="hljs-function"&gt;(&lt;span&gt;resolve, reject&lt;/span&gt;)=&amp;gt;&lt;/span&gt; {
        &lt;span class="hljs-built_in"&gt;setTimeout&lt;/span&gt;(resolve, &lt;span class="hljs-number"&gt;3000&lt;/span&gt;, &lt;span class="hljs-string"&gt;"task 3"&lt;/span&gt;)
    }),
]
&lt;span class="hljs-built_in"&gt;Promise&lt;/span&gt;.all(tasks).then(&lt;span class="hljs-function"&gt;&lt;span&gt;data&lt;/span&gt; =&amp;gt;&lt;/span&gt; {
    &lt;span class="hljs-built_in"&gt;console&lt;/span&gt;.log(data); &lt;span class="hljs-comment"&gt;// ["task 1", "task 2", "task 3"]&lt;/span&gt;
})
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;If any one of the promises gets rejected then it immediately stops the execution and rejects with the error returned from the rejected promise.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&lt;span class="hljs-keyword"&gt;const&lt;/span&gt; tasks = [
    &lt;span class="hljs-keyword"&gt;new&lt;/span&gt; &lt;span class="hljs-built_in"&gt;Promise&lt;/span&gt;(&lt;span class="hljs-function"&gt;(&lt;span&gt;resolve, reject&lt;/span&gt;)=&amp;gt;&lt;/span&gt; {
        &lt;span class="hljs-built_in"&gt;setTimeout&lt;/span&gt;(resolve, &lt;span class="hljs-number"&gt;1000&lt;/span&gt;, &lt;span class="hljs-string"&gt;"task 1"&lt;/span&gt;)
    }),
    &lt;span class="hljs-keyword"&gt;new&lt;/span&gt; &lt;span class="hljs-built_in"&gt;Promise&lt;/span&gt;(&lt;span class="hljs-function"&gt;(&lt;span&gt;resolve, reject&lt;/span&gt;)=&amp;gt;&lt;/span&gt; {
        &lt;span class="hljs-built_in"&gt;setTimeout&lt;/span&gt;(reject, &lt;span class="hljs-number"&gt;2000&lt;/span&gt;, &lt;span class="hljs-string"&gt;"task 2 failed"&lt;/span&gt;)
    }),
    &lt;span class="hljs-keyword"&gt;new&lt;/span&gt; &lt;span class="hljs-built_in"&gt;Promise&lt;/span&gt;(&lt;span class="hljs-function"&gt;(&lt;span&gt;resolve, reject&lt;/span&gt;)=&amp;gt;&lt;/span&gt; {
        &lt;span class="hljs-built_in"&gt;setTimeout&lt;/span&gt;(resolve, &lt;span class="hljs-number"&gt;3000&lt;/span&gt;, &lt;span class="hljs-string"&gt;"task 3"&lt;/span&gt;)
    }),
]
&lt;span class="hljs-built_in"&gt;Promise&lt;/span&gt;.all(tasks).then(&lt;span class="hljs-function"&gt;&lt;span&gt;data&lt;/span&gt; =&amp;gt;&lt;/span&gt; {
    &lt;span class="hljs-built_in"&gt;console&lt;/span&gt;.log(data);
}).catch(&lt;span class="hljs-function"&gt;&lt;span&gt;error&lt;/span&gt; =&amp;gt;&lt;/span&gt; {
    &lt;span class="hljs-built_in"&gt;console&lt;/span&gt;.log(error); &lt;span class="hljs-comment"&gt;// task 2 failed&lt;/span&gt;
})
&lt;/code&gt;&lt;/pre&gt;

&lt;h3 id="heading-promiseallsettled-method"&gt;
&lt;code&gt;Promise.allSettled&lt;/code&gt; method&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;Promise.allSettled&lt;/code&gt; iterates over an array of promises and waits for all the promises to get either resolved or rejected. It resolves to an array of data with each promise's status in the following format.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;[
  {&lt;span class="hljs-attr"&gt;status&lt;/span&gt;: &lt;span class="hljs-string"&gt;"fulfilled"&lt;/span&gt;, &lt;span class="hljs-attr"&gt;value&lt;/span&gt;: value1},
  {&lt;span class="hljs-attr"&gt;status&lt;/span&gt;: &lt;span class="hljs-string"&gt;"fulfilled"&lt;/span&gt;, &lt;span class="hljs-attr"&gt;value&lt;/span&gt;: value2},
  {&lt;span class="hljs-attr"&gt;status&lt;/span&gt;: &lt;span class="hljs-string"&gt;"rejected"&lt;/span&gt;,  &lt;span class="hljs-attr"&gt;reason&lt;/span&gt;: error}
]
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Let's see what will be the output of the &lt;code&gt;tasks2&lt;/code&gt; &lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&lt;span class="hljs-keyword"&gt;const&lt;/span&gt; tasks = [
    &lt;span class="hljs-keyword"&gt;new&lt;/span&gt; &lt;span class="hljs-built_in"&gt;Promise&lt;/span&gt;(&lt;span class="hljs-function"&gt;(&lt;span&gt;resolve, reject&lt;/span&gt;)=&amp;gt;&lt;/span&gt; {
        &lt;span class="hljs-built_in"&gt;setTimeout&lt;/span&gt;(resolve, &lt;span class="hljs-number"&gt;1000&lt;/span&gt;, &lt;span class="hljs-string"&gt;"task 1"&lt;/span&gt;)
    }),
    &lt;span class="hljs-keyword"&gt;new&lt;/span&gt; &lt;span class="hljs-built_in"&gt;Promise&lt;/span&gt;(&lt;span class="hljs-function"&gt;(&lt;span&gt;resolve, reject&lt;/span&gt;)=&amp;gt;&lt;/span&gt; {
        &lt;span class="hljs-built_in"&gt;setTimeout&lt;/span&gt;(reject, &lt;span class="hljs-number"&gt;2000&lt;/span&gt;, &lt;span class="hljs-string"&gt;"task 2 failed"&lt;/span&gt;)
    }),
    &lt;span class="hljs-keyword"&gt;new&lt;/span&gt; &lt;span class="hljs-built_in"&gt;Promise&lt;/span&gt;(&lt;span class="hljs-function"&gt;(&lt;span&gt;resolve, reject&lt;/span&gt;)=&amp;gt;&lt;/span&gt; {
        &lt;span class="hljs-built_in"&gt;setTimeout&lt;/span&gt;(resolve, &lt;span class="hljs-number"&gt;3000&lt;/span&gt;, &lt;span class="hljs-string"&gt;"task 3"&lt;/span&gt;)
    }),
]
&lt;span class="hljs-built_in"&gt;Promise&lt;/span&gt;.allSettled(tasks).then(&lt;span class="hljs-function"&gt;&lt;span&gt;data&lt;/span&gt; =&amp;gt;&lt;/span&gt; {
    &lt;span class="hljs-built_in"&gt;console&lt;/span&gt;.log(data);
})

&lt;span class="hljs-comment"&gt;// Output&lt;/span&gt;
[
  {
    &lt;span class="hljs-attr"&gt;status&lt;/span&gt;: &lt;span class="hljs-string"&gt;'fulfilled'&lt;/span&gt;,
    &lt;span class="hljs-attr"&gt;value&lt;/span&gt;: &lt;span class="hljs-string"&gt;'task 1'&lt;/span&gt;,
  },
  {
    &lt;span class="hljs-attr"&gt;status&lt;/span&gt;: &lt;span class="hljs-string"&gt;'rejected'&lt;/span&gt;,
    &lt;span class="hljs-attr"&gt;reason&lt;/span&gt;: &lt;span class="hljs-string"&gt;'task 2 failed'&lt;/span&gt;,
  },
  {
    &lt;span class="hljs-attr"&gt;status&lt;/span&gt;: &lt;span class="hljs-string"&gt;'fulfilled'&lt;/span&gt;,
    &lt;span class="hljs-attr"&gt;value&lt;/span&gt;: &lt;span class="hljs-string"&gt;'task 3'&lt;/span&gt;,
  },
];
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;This article is published w/ &lt;a href="https://scattr.io?ref=dev"&gt;Scattr  ↗️&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Debugging a Production Issue: Lessons Learned from Solving a Date Parsing Bug in Chromium v110</title>
      <dc:creator>Karan Kiri</dc:creator>
      <pubDate>Wed, 15 Mar 2023 14:18:46 +0000</pubDate>
      <link>https://dev.to/karankiri/debugging-a-production-issue-lessons-learned-from-solving-a-date-parsing-bug-in-chromium-v110-3ho6</link>
      <guid>https://dev.to/karankiri/debugging-a-production-issue-lessons-learned-from-solving-a-date-parsing-bug-in-chromium-v110-3ho6</guid>
      <description>&lt;p&gt;Debugging a production issue can be a daunting task, but it's also an opportunity to learn new things and improve your skills as a developer. Recently, We encountered a production issue where one of the web pages in our application was not working. The issue turned out to be caused by a change in the behaviour of the &lt;code&gt;Date.toLocaleString&lt;/code&gt; method, which was used to get AM/PM information. Let's start going through the events, and how it was reported to finally be fixed.&lt;/p&gt;

&lt;h3 id="heading-the-problem"&gt;The Problem&lt;/h3&gt;

&lt;p&gt;One day, we received a support ticket stating that a particular webpage in our application was not working. The page was responsible for inviting new users to the particular account and displaying the details of all the users including when the user was created.&lt;/p&gt;

&lt;p&gt;The first step in debugging any production issue is to gather as much information as possible about the problem and try to reproduce the issue locally.&lt;/p&gt;

&lt;p&gt;I tried to reproduce the same issue on my machine but it was working fine. I asked my teammate to check if it was working for them. They were able to reproduce the issue. So we checked the console logs and found that there was an error in the Date parsing logic.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;The initial instinct was that this could be an API/Data issue as we haven't done any deployment on the Frontend for some time.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;We compared the date format from both browsers where it was working and where it wasn't and it was the same.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now, the problem seemed weird because it was not happening for all the users. So we decide to check that on different browsers and found that the issue was happening for users on Chromium version &amp;gt;= 110, and it was working for all other browsers and Chromiums version below 110.&lt;/p&gt;

&lt;p&gt;In the meantime, we communicated with the reporter that they can use other browsers to get unblocked while we debug the issue.&lt;/p&gt;

&lt;p&gt;At this point, we knew something was broken in Chromium v110 but were still not sure what was breaking.&lt;/p&gt;

&lt;p&gt;The next step was to run the same piece of code on the browser where it was failing, below is the sample piece of code that was failing in Chromium v110 with the following error&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Uncaught TypeError: Cannot read properties of undefined (reading 'toLocaleUpperCase')&lt;/code&gt;&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&lt;span class="hljs-keyword"&gt;const&lt;/span&gt; date = &lt;span class="hljs-keyword"&gt;new&lt;/span&gt; &lt;span class="hljs-built_in"&gt;Date&lt;/span&gt;(createdAt);
&lt;span class="hljs-keyword"&gt;const&lt;/span&gt; dateArray = date.toLocaleString([], { &lt;span class="hljs-attr"&gt;hour12&lt;/span&gt;: &lt;span class="hljs-literal"&gt;true&lt;/span&gt; }).split(&lt;span class="hljs-string"&gt;' '&lt;/span&gt;);
&lt;span class="hljs-keyword"&gt;const&lt;/span&gt; time = dateArray[&lt;span class="hljs-number"&gt;1&lt;/span&gt;];
&lt;span class="hljs-keyword"&gt;const&lt;/span&gt; ampm = dateArray[&lt;span class="hljs-number"&gt;2&lt;/span&gt;].toLocaleUpperCase();
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now, was the time to check the value of &lt;code&gt;dateArray&lt;/code&gt; in both browsers, the output was something like this&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&lt;span class="hljs-keyword"&gt;const&lt;/span&gt; date = &lt;span class="hljs-keyword"&gt;new&lt;/span&gt; &lt;span class="hljs-built_in"&gt;Date&lt;/span&gt;(createdAt);
&lt;span class="hljs-keyword"&gt;const&lt;/span&gt; dateArray = date.toLocaleString([], { &lt;span class="hljs-attr"&gt;hour12&lt;/span&gt;: &lt;span class="hljs-literal"&gt;true&lt;/span&gt; }).split(&lt;span class="hljs-string"&gt;' '&lt;/span&gt;);

&lt;span class="hljs-comment"&gt;// ['09/03/2023,', '0:47:09 am'] - Chromium 110&lt;/span&gt;
&lt;span class="hljs-comment"&gt;// ['09/03/2023,', '0:47:09', 'am'] - Other browsers&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now we're able to figure out that something is wrong with &lt;code&gt;toLocaleString&lt;/code&gt; , to finally confirm the issue, We ran the below piece of code in both browsers.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&lt;span class="hljs-keyword"&gt;const&lt;/span&gt; dateString = date.toLocaleString([], { &lt;span class="hljs-attr"&gt;hour12&lt;/span&gt;: &lt;span class="hljs-literal"&gt;true&lt;/span&gt; });
dateString.charCodeAt(&lt;span class="hljs-number"&gt;19&lt;/span&gt;);

&lt;span class="hljs-comment"&gt;// 8239 - Chromium 110&lt;/span&gt;
&lt;span class="hljs-comment"&gt;// 32 - Other browsers&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This confirmed that the bug was in the Chromium update and we were able to find the same bug reported in the Chromium project here &lt;a href="https://crbug.com/1414553"&gt;https://crbug.com/1414553&lt;/a&gt;&lt;/p&gt;

&lt;h3 id="heading-the-solution"&gt;The Solution&lt;/h3&gt;

&lt;p&gt;After identifying the root cause of the problem, We began to explore possible solutions. One solution was to update our code to use a different method for parsing the AM/PM information. Since we were using the &lt;code&gt;date-fns&lt;/code&gt; library in our project, it was a pretty straightforward way to use the format function from the library and fix the issue&lt;/p&gt;

&lt;p&gt;Here's an example of how the updated code looked like:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&lt;span class="hljs-keyword"&gt;try&lt;/span&gt; {
    &lt;span class="hljs-keyword"&gt;return&lt;/span&gt; format(createdAt, &lt;span class="hljs-string"&gt;'yyyy-MM-dd hh:mm:ss a'&lt;/span&gt;) || &lt;span class="hljs-string"&gt;''&lt;/span&gt;;
  } &lt;span class="hljs-keyword"&gt;catch&lt;/span&gt; (error) {
    &lt;span class="hljs-keyword"&gt;return&lt;/span&gt; &lt;span class="hljs-string"&gt;''&lt;/span&gt;;
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;After making these changes, We tested the fix in a staging environment and confirmed that the issue was resolved. We then deployed the fix to the production environment and monitored the logs to ensure that the error did not occur again.&lt;/p&gt;

&lt;h3 id="heading-lessons-learned"&gt;Lessons Learned&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Always make sure error handling is done properly in your code, in our case this led to the blank page&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Never rely on a browser &lt;code&gt;toLocaleString&lt;/code&gt; to parse date information, rather than use the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/formatToParts"&gt;formatToParts&lt;/a&gt; method which is reliable for parsing such information or you can use a dedicated library like &lt;code&gt;date-fns&lt;/code&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id="heading-conclusion"&gt;Conclusion&lt;/h3&gt;

&lt;p&gt;In conclusion, debugging production issues can be challenging, but it's important to approach the problem systematically and gather as much information as possible before attempting to fix it. In this case, the issue was caused by a change in the behaviour of the &lt;code&gt;Date.toLocaleString&lt;/code&gt; method, which required a code update to handle the new behaviour. By understanding the root cause of the issue and implementing a proper fix, We were able to restore the functionality of the affected web page and prevent the issue from happening again in the future.&lt;/p&gt;


&lt;p&gt;This article is published w/ &lt;a href="https://scattr.io?ref=dev"&gt;Scattr  ↗️&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
