<?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: Piotr Nalepa</title>
    <description>The latest articles on DEV Community by Piotr Nalepa (@sunpietro).</description>
    <link>https://dev.to/sunpietro</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%2F187016%2F6947cc5f-6525-4ef0-80d6-8cf777940926.png</url>
      <title>DEV Community: Piotr Nalepa</title>
      <link>https://dev.to/sunpietro</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sunpietro"/>
    <language>en</language>
    <item>
      <title>Git merge vs git rebase - why you should avoid using Git merge to update your branches?</title>
      <dc:creator>Piotr Nalepa</dc:creator>
      <pubDate>Tue, 20 Sep 2022 09:42:47 +0000</pubDate>
      <link>https://dev.to/sunpietro/git-merge-vs-git-rebase-why-you-should-avoid-using-git-merge-to-update-your-branches-5df8</link>
      <guid>https://dev.to/sunpietro/git-merge-vs-git-rebase-why-you-should-avoid-using-git-merge-to-update-your-branches-5df8</guid>
      <description>&lt;p&gt;Git is a the most popular version control system in the software engineering world. Its popularity grown even bigger with the raise of Github, Gitlab, Bitbucket and similar tools. When you think about software development Git is a crucial part of it, you just can’t live without it if you’re doing it professionally.&lt;/p&gt;

&lt;h2&gt;
  
  
  Background
&lt;/h2&gt;

&lt;p&gt;Imagine you’re working on some big feature that requires a lot of time to implement it properly. In the meantime your team members are developing their own tasks. They get reviewed and merged to to the branch: &lt;code&gt;master&lt;/code&gt;, &lt;code&gt;main&lt;/code&gt;, &lt;code&gt;develop&lt;/code&gt; - you name it (I'm going to refer to it as a source branch later in the text).&lt;/p&gt;

&lt;p&gt;At some point you realize that it would be good to update your working branch with the latest changes from the source branch. It can be tempting to do it by either running &lt;code&gt;git merge &amp;lt;source-branch-name&amp;gt;&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Update with merge commit&lt;/strong&gt; option. After doing it the list of commits is increased by a number of merge commits.&lt;/p&gt;

&lt;p&gt;Notice, there are commits starting with &lt;strong&gt;Merge branch ‘develop’ into …&lt;/strong&gt; those are the merge commits, but if you take a look at the branch history on Git it looks even more messy.&lt;/p&gt;

&lt;p&gt;In the Github UI there are 12 commits, but when you’re looking into the Git history you can see there are 19 commits in total.&lt;/p&gt;

&lt;h2&gt;
  
  
  Git merge is a bad idea
&lt;/h2&gt;

&lt;p&gt;You might be wondering what’s the problem there? IMO, it’s bad because it messes up your work with someone else’s work. Mixing commits leads to confusion when trying to find a specific commit that introduced a specific bug or trying to squash all your commits into one with &lt;code&gt;git rebase -i HEAD~&amp;lt;number-of-your-commits&amp;gt;&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;In general, your work should be always on top of the source branch commits so you can easily find out what does your work contain and, what’s more important, you can have a flat commits history in your branch. In the last picture there are 3 levels of commits caused by using &lt;code&gt;git merge&lt;/code&gt;. Which can be problematic to read sometimes. More nesting levels mean more complex graphs to read and to understand the history of changes.&lt;/p&gt;

&lt;p&gt;Using &lt;code&gt;git rebase&lt;/code&gt; will help you:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;To have a clear list of changes in your repository. You’ll know what change is an effect of another.&lt;/li&gt;
&lt;li&gt;To have a linear history of changes. No nested levels of commits making it difficult to read.&lt;/li&gt;
&lt;li&gt;To debug your code easier with &lt;code&gt;git bisect&lt;/code&gt;. This command is really cool to find the exact commit that broke the code. With that you can mark which commits are good and which are broken. You can read more about it here: &lt;a href="https://git-scm.com/docs/git-bisect"&gt;https://git-scm.com/docs/git-bisect&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What can you do instead?
&lt;/h2&gt;

&lt;p&gt;To learn more about what can you do instead go to &lt;a href="https://blog.piotrnalepa.pl/2022/09/19/git-merge-vs-git-rebase-why-you-should-avoid-using-git-merge-to-update-your-branches/"&gt;the full version of this article&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>git</category>
      <category>github</category>
      <category>devlife</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Dealing with Promise.all() and a bunch of async functions</title>
      <dc:creator>Piotr Nalepa</dc:creator>
      <pubDate>Thu, 02 Jul 2020 05:55:08 +0000</pubDate>
      <link>https://dev.to/sunpietro/dealing-with-promise-all-and-a-bunch-of-async-functions-5357</link>
      <guid>https://dev.to/sunpietro/dealing-with-promise-all-and-a-bunch-of-async-functions-5357</guid>
      <description>&lt;p&gt;Recently I've been in the situation where I needed to resolve multiple async functions in paralel. The tricky part was that these functions were written using &lt;strong&gt;async/await&lt;/strong&gt; approach and I was going to use &lt;code&gt;Promise.all()&lt;/code&gt; function to resolve all of the async requests at the same time, when all of them are resolved.&lt;/p&gt;

&lt;p&gt;I'm not going to tell you much about Promises and async/await. I assume you have already gained proper knowledge regarding how to use them in your project.&lt;/p&gt;

&lt;h3&gt;The first approach&lt;/h3&gt;

&lt;p&gt;In order to handle such situation where I had a bunch of async functions I managed to put them all into an array and use it as a param of &lt;code&gt;Promise.all()&lt;/code&gt; function. Just like that:&lt;/p&gt;

&lt;pre&gt;Promise.all([
    await dispatch(fetchDataFromOneSource),
    await dispatch(fetchDataFromAnotherSource)
])
.then([data1, data2] =&amp;gt; {})
.catch(error =&amp;gt; console.log(error))
&lt;/pre&gt;

&lt;p&gt;The code listing above is simplified in order to focus on the most important part.&lt;/p&gt;

&lt;p&gt;As you can see, I'm using:&lt;/p&gt;

&lt;pre&gt;[
    await dispatch(fetchDataFromOneSource),
    await dispatch(fetchDataFromAnotherSource)
]
&lt;/pre&gt;

&lt;p&gt;as an input param of &lt;code&gt;Promise.all()&lt;/code&gt;. As we all know, &lt;code&gt;async/await&lt;/code&gt; approach is just a syntax sugar for Promises, so I expected to have all promises resolved when data is ready. It works perfectly fine, when all promises are resolved correctly. The &lt;code&gt;then()&lt;/code&gt; part is run and everyone is happy.&lt;/p&gt;

&lt;p&gt;In my case, there were specific situations when one of the async functions should fail and this should prevent from running a callback of &lt;code&gt;then()&lt;/code&gt; part. I expected that &lt;code&gt;catch()&lt;/code&gt; will be invoked instead.&lt;br&gt;
I was so wrong! All of it failed silently and except showing errors in the browser console nothing happened in the UI, while it should!&lt;/p&gt;

&lt;h3&gt;The second approach&lt;/h3&gt;

&lt;p&gt;At the time being I was rushed by the deadline and I've come up with a following solution:&lt;/p&gt;

&lt;pre&gt;Promise.all([
    await dispatch(fetchDataFromOneSource)
        .catch(handleError),
    await dispatch(fetchDataFromAnotherSource)
        .catch(handleError)
])
.then([data1, data2] =&amp;gt; {})
.catch(handleError)
&lt;/pre&gt;

&lt;p&gt;It solved my problem but it was not the most elegant solution in the world. It bothered me a lot. Duplicated error handlers for each promise was not the most optimal solution in my case.&lt;/p&gt;

&lt;h3&gt;The final approach&lt;/h3&gt;

&lt;p&gt;Finally, after thinking for hours. I realised where was the mistake and how to make the previous version more elegant:&lt;/p&gt;

&lt;pre&gt;await Promise.all([
    dispatch(fetchDataFromOneSource),
    dispatch(fetchDataFromAnotherSource)
])
.then([data1, data2] =&amp;gt; {})
.catch(error =&amp;gt; console.log(error))
&lt;/pre&gt;

&lt;p&gt;This also worked as previously, but the final piece of code looks better and it's less hackish now.&lt;/p&gt;

&lt;p&gt;It's worth to mention, that running &lt;code&gt;await Promise.all()&lt;/code&gt; allows you to assign its output to a variable and maybe destructure it, if needed, and then proceed with next actions instead of running the &lt;code&gt;then()&lt;/code&gt; callback param.&lt;/p&gt;

&lt;h3&gt;Summary&lt;/h3&gt;

&lt;p&gt;By writing this article I wanted to clarify the statement I wrote on Twitter:&lt;/p&gt;


&lt;blockquote&gt;
&lt;p&gt;TIL: If any promise in Promise.all() will fail then catch assigned to Promise.all won’t work. You have to add catch to all promises in the array of promises used a Promise.all param. &lt;a href="https://twitter.com/hashtag/javascript?src=hash&amp;amp;ref_src=twsrc%5Etfw"&gt;#javascript&lt;/a&gt;&lt;/p&gt;— Piotr Nalepa (&lt;a class="mentioned-user" href="https://dev.to/sunpietro"&gt;@sunpietro&lt;/a&gt;) &lt;a href="https://twitter.com/sunpietro/status/1239891678565187584?ref_src=twsrc%5Etfw"&gt;March 17, 2020&lt;/a&gt;
&lt;/blockquote&gt;  

&lt;p&gt;&lt;strong&gt;I was wrong then. The catch will be invoked when the array of Promises contain the Promise objects itself, not the invokations of async functions.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I hope this article clarified it and it will help you solving issues in your projects.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>async</category>
      <category>promise</category>
    </item>
  </channel>
</rss>
