<?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>Forem: re4388</title>
    <description>The latest articles on Forem by re4388 (@re4388).</description>
    <link>https://forem.com/re4388</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%2F112640%2F2bd63a72-9e2e-4c9e-9c72-f7bed15bf217.png</url>
      <title>Forem: re4388</title>
      <link>https://forem.com/re4388</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/re4388"/>
    <language>en</language>
    <item>
      <title>Let's practice Insert Sort (Javascript)</title>
      <dc:creator>re4388</dc:creator>
      <pubDate>Sat, 06 Aug 2022 01:10:52 +0000</pubDate>
      <link>https://forem.com/re4388/lets-practice-insert-sort-javascript-5790</link>
      <guid>https://forem.com/re4388/lets-practice-insert-sort-javascript-5790</guid>
      <description>&lt;h2&gt;
  
  
  Show me the code
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&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="mi"&gt;9&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;7&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&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;function&lt;/span&gt; &lt;span class="nx"&gt;InsertSort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&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;let&lt;/span&gt; &lt;span class="nx"&gt;currentIdx&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;j&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;j&lt;/span&gt; &lt;span class="o"&gt;&amp;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;j&lt;/span&gt;&lt;span class="o"&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;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;currentIdx&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;j&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;currentIdx&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;j&lt;/span&gt;&lt;span class="p"&gt;]]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;j&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;currentIdx&lt;/span&gt;&lt;span class="p"&gt;]]&lt;/span&gt;
                &lt;span class="nx"&gt;currentIdx&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;j&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;return&lt;/span&gt; &lt;span class="nx"&gt;arr&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="nx"&gt;InsertSort&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="c1"&gt;//[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  key Idea
&lt;/h2&gt;

&lt;p&gt;The tricky part of this algorithm:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Define a &lt;code&gt;currentIdx&lt;/code&gt; variable, which is the index the latter will be compared to.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The second nested for-loop will have the &lt;code&gt;j&lt;/code&gt; index and compare to the &lt;code&gt;currentIdx&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Since &lt;code&gt;currentIdx&lt;/code&gt; is already assigned to the current Index, &lt;code&gt;i&lt;/code&gt;, so the &lt;code&gt;j&lt;/code&gt; will be &lt;code&gt;j-1&lt;/code&gt;, the previous one, we want to compare. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We compare two number side by side. This is part of how this algoritem works.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Finally, when the condition match (meaning the &lt;code&gt;arr[currentIdx] &amp;lt; arr[j]&lt;/code&gt;), we swap and we need to dynamically rebind &lt;code&gt;currentIdx&lt;/code&gt; to &lt;code&gt;j&lt;/code&gt;. This is the essential part of this algorithm, which is going to find the previous index spot and swap (or "insert")  them.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Tip: Try to google "insert sort animation" and have a visual idea of how the algorithm goes.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>algorithms</category>
      <category>javascript</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
    <item>
      <title>Git small tip: one way to handle conflict if you only need is the latest remote code.</title>
      <dc:creator>re4388</dc:creator>
      <pubDate>Tue, 02 Aug 2022 08:16:17 +0000</pubDate>
      <link>https://forem.com/re4388/git-small-tip-one-way-to-handle-conflict-if-you-only-need-is-the-latest-remote-code-3cdf</link>
      <guid>https://forem.com/re4388/git-small-tip-one-way-to-handle-conflict-if-you-only-need-is-the-latest-remote-code-3cdf</guid>
      <description>&lt;p&gt;This trick I use time by time, and I want to share it with you.&lt;/p&gt;

&lt;p&gt;Sometimes when I review someone’s code, the code got updated remotely. So  I &lt;code&gt;git pull&lt;/code&gt;, and I just found out there are a lot of conflicts, and it’s really hard to resolve these conflicts.&lt;/p&gt;

&lt;p&gt;I don’t want to resolve those conflicts, and I just want the latest code! What can you do?&lt;/p&gt;

&lt;p&gt;Maybe the easiest but also time-consuming way is to git clone the new repo again. I know the new repo will have the latest one and then I can switch to that branch.&lt;/p&gt;




&lt;p&gt;And below is another way I learned and way quicker:&lt;br&gt;
Since the problem is about the branch, so we can just remove the branch!&lt;/p&gt;



&lt;p&gt;Firstly, we can just commit all changes whatsoever (regardless of the conflict, just clean up the git staging, and revert all changes).&lt;br&gt;
Then we switch to master, and then remove this broken local branch. Finally, I can git pull again and switch back and get the latest update from that branch!&lt;/p&gt;



&lt;p&gt;See the below snippet:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# just git commit all stuff whatsoever&lt;/span&gt;
git add &lt;span class="nb"&gt;.&lt;/span&gt;
git commit &lt;span class="nt"&gt;-m&lt;/span&gt; &lt;span class="s1"&gt;'temp'&lt;/span&gt;

&lt;span class="c"&gt;# go master&lt;/span&gt;
git checkout master 

&lt;span class="c"&gt;# find the branch in case you want to copy paste the branch (beanch name could be lengthy)&lt;/span&gt;
git branch

&lt;span class="c"&gt;# delete it&lt;/span&gt;
git branch &lt;span class="nt"&gt;-D&lt;/span&gt; branchName_1_2_3


&lt;span class="c"&gt;# pull the latest one and check out to that brancg again, problem solved!&lt;/span&gt;
git pull 
git branch branchName_1_2_3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let me know if you have other thoughts on this!&lt;br&gt;
Thanks for reading!&lt;/p&gt;

</description>
      <category>git</category>
      <category>webdev</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
    <item>
      <title>Can you answer this simple JS async interview question?</title>
      <dc:creator>re4388</dc:creator>
      <pubDate>Mon, 01 Aug 2022 12:39:48 +0000</pubDate>
      <link>https://forem.com/re4388/can-you-answer-this-simple-js-async-interview-question-2l1l</link>
      <guid>https://forem.com/re4388/can-you-answer-this-simple-js-async-interview-question-2l1l</guid>
      <description>&lt;h2&gt;
  
  
  See the blow question:
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;promise&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;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="mi"&gt;1&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="mi"&gt;5&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="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}).&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;val&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;val&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="nx"&gt;promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&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="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;6&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;What will log out in order?&lt;/p&gt;

&lt;p&gt;wait.&lt;/p&gt;




&lt;p&gt;wait..&lt;/p&gt;




&lt;p&gt;wait...&lt;/p&gt;




&lt;p&gt;Okay.. here you are :)&lt;/p&gt;

&lt;p&gt;--&lt;/p&gt;

&lt;h2&gt;
  
  
  Answer:
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://gist.github.com/re4388/3ff061f378d2f153f9f24a2079b1aca0"&gt;See this gist for answer&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Explain:
&lt;/h2&gt;

&lt;p&gt;We only need to remeber the prioirty is like this:&lt;br&gt;
&lt;strong&gt;sync code -&amp;gt; micro tasks -&amp;gt; macro task&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;See this &lt;a href="https://javascript.info/event-loop"&gt;https://javascript.info/event-loop&lt;/a&gt; if you want to know why&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;First of all, What are the synchronous code runs in order? &lt;br&gt;
1, 2, 4&lt;/p&gt;

&lt;p&gt;Don't be fooled if you think the 1 and 2 inside the new Promise block is async code, these two console.log are sync code.&lt;/p&gt;

&lt;p&gt;When we new up a new Promise, we immediately call those and only the resolve callback (and reject callback) are async.&lt;/p&gt;



&lt;p&gt;Secondly, what are the mico tasks runs in order? 5, 3&lt;/p&gt;

&lt;p&gt;Both of them are promise micro tasks and we know those belong to micro task&lt;/p&gt;



&lt;p&gt;Finally, what's the macro task? 6&lt;/p&gt;

&lt;p&gt;And that's the order!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;promise&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;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="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;   &lt;span class="c1"&gt;// &amp;lt;-- sync code 1&lt;/span&gt;
    &lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="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="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;   &lt;span class="c1"&gt;// &amp;lt;-- sync code 2&lt;/span&gt;
&lt;span class="p"&gt;}).&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;val&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;val&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// &amp;lt;-- micro code 1, log 5&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="nx"&gt;promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;   &lt;span class="c1"&gt;// &amp;lt;-- micro code 2&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="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;       &lt;span class="c1"&gt;// &amp;lt;-- sync code 3&lt;/span&gt;

&lt;span class="nx"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;   &lt;span class="c1"&gt;// &amp;lt;-- macro code&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 for today :) &lt;/p&gt;

</description>
      <category>javascript</category>
      <category>programming</category>
      <category>interview</category>
      <category>webdev</category>
    </item>
    <item>
      <title>This maybe all you need to know to use Google Keep efficenty!</title>
      <dc:creator>re4388</dc:creator>
      <pubDate>Sun, 31 Jul 2022 06:17:28 +0000</pubDate>
      <link>https://forem.com/re4388/this-maybe-all-you-need-to-know-to-use-google-keep-efficenty-327m</link>
      <guid>https://forem.com/re4388/this-maybe-all-you-need-to-know-to-use-google-keep-efficenty-327m</guid>
      <description>&lt;ol&gt;
&lt;li&gt;&lt;p&gt;use &lt;code&gt;?&lt;/code&gt; to find the shortcut you want to use.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Remember that you can pin stuff to help you focus&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;think about something you want to see every time you open the note)&lt;/p&gt;
&lt;/blockquote&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;When you are in the browser, you can always use &lt;code&gt;note.new&lt;/code&gt; to go to the Keep and open the new todo for you to type in!&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Bonus: use &lt;code&gt;ctrl+l&lt;/code&gt; to go to the address bar in chrome)&lt;/p&gt;
&lt;/blockquote&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;use hash &lt;code&gt;#&lt;/code&gt; to manage your note by adding &lt;code&gt;#&lt;/code&gt; at the bottom of the note&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;You can just create the tag first.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;If you have another dimension that wants to categorize your notes, use color! &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;You then can use the below link to go to notes of the color.&lt;br&gt;
&lt;code&gt;https://keep.google.com/u/0/#search/color%3Dred&lt;/code&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;see the pattern? if you want to go to the yellow page, change the last 3 characters to yellow, like this: &lt;code&gt;https://keep.google.com/u/0/#search/color%3Dyellow&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Finally, if you are a more video learning person, check the below vid and I learn all from it!&lt;br&gt;
The Author also has some very good tricks on other topics, I think it's worth checking those out :)&lt;br&gt;
&lt;a href="https://www.youtube.com/watch?v=-jCyuBLLcvs"&gt;https://www.youtube.com/watch?v=-jCyuBLLcvs&lt;/a&gt;&lt;/p&gt;

</description>
      <category>productivity</category>
      <category>beginners</category>
      <category>cheatsheet</category>
    </item>
    <item>
      <title>Use Rxjs takeUntil to unsubscribe</title>
      <dc:creator>re4388</dc:creator>
      <pubDate>Fri, 19 Mar 2021 11:58:32 +0000</pubDate>
      <link>https://forem.com/re4388/use-rxjs-takeuntil-to-unsubscribe-1ffj</link>
      <guid>https://forem.com/re4388/use-rxjs-takeuntil-to-unsubscribe-1ffj</guid>
      <description>&lt;h2&gt;
  
  
  Why do we need to &lt;strong&gt;Unsubscribe&lt;/strong&gt;?
&lt;/h2&gt;

&lt;p&gt;The subscription will keep reference the component instance &lt;strong&gt;even after&lt;/strong&gt; the component is destroyed if you DO NOT unsubscribe in some cases.&lt;/p&gt;

&lt;p&gt;So, you have a piece of memory that is not be garbage collected (Memory leak).&lt;/p&gt;

&lt;p&gt;Not Good. The app will slow down if we do a lot of this kind of thing.&lt;/p&gt;

&lt;h2&gt;
  
  
  Angular will help us to unsubscribe?
&lt;/h2&gt;

&lt;p&gt;Yes, as I know, these are cases:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;AsyncPipe&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;HTTP&lt;/code&gt; service&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;timer&lt;/code&gt; observable&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let me know if you know more cases :)&lt;/p&gt;

&lt;h2&gt;
  
  
  So, we need to unsubscribe on other cases
&lt;/h2&gt;




&lt;h3&gt;
  
  
  1. The more straightforward way:
&lt;/h3&gt;

&lt;p&gt;You make a subscription, and you shall unsubscribe it.&lt;/p&gt;

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

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MyClass&lt;/span&gt; &lt;span class="k"&gt;implements&lt;/span&gt; &lt;span class="nx"&gt;OnInit&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;OnDestroy&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="nx"&gt;subscription&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Subscription&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nx"&gt;timer$&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Observable&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kr"&gt;number&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;ngOnInit&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;subscription&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;timer$&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;subscribe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;t&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="nx"&gt;t&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;ngOnDestroy&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;subscription&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;unsubscribe&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;



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

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Pro?&lt;/strong&gt; Easy to understand.&lt;br&gt;
&lt;strong&gt;Con?&lt;/strong&gt; You or others who work on the same code base might forget to unsubscribe.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. The &lt;code&gt;takeUntil&lt;/code&gt; way
&lt;/h3&gt;


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

&lt;p&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MyClass&lt;/span&gt; &lt;span class="k"&gt;implements&lt;/span&gt; &lt;span class="nx"&gt;OnInit&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;OnDestroy&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;br&gt;
  &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="nx"&gt;unsubscribe$&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Subject&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;void&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&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;Subject&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;&lt;br&gt;
  &lt;span class="nx"&gt;timer$&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Observable&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;&lt;span class="nf"&gt;ngOnInit&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;br&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;timer$&lt;/span&gt;&lt;br&gt;
      &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pipe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;takeUntil&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;unsubscribe$&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;&lt;br&gt;
      &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;subscribe&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;t&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="nx"&gt;t&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;&lt;br&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;&lt;span class="nf"&gt;ngOnDestroy&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;br&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;unsubscribe$&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;next&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;&lt;br&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;unsubscribe$&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;complete&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;&lt;br&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;&lt;br&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;/p&gt;

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

&lt;/div&gt;
&lt;h3&gt;
&lt;br&gt;
  &lt;br&gt;
  &lt;br&gt;
  What &lt;code&gt;takeUntil&lt;/code&gt; do?&lt;br&gt;
&lt;/h3&gt;

&lt;p&gt;I think this picture said it all!&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%2Fpbwc4st2zdder5q7f0e8.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%2Fpbwc4st2zdder5q7f0e8.png" alt="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pbwc4st2zdder5q7f0e8.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;z&lt;/code&gt; ball is like the above "Subject"&lt;/p&gt;

&lt;p&gt;when z . next,  ⇒ &lt;code&gt;takeUntil&lt;/code&gt; will "Stop" the source Observable&lt;/p&gt;

&lt;p&gt;As a result, the subscription method will "complete" when the &lt;code&gt;unsubscribe$&lt;/code&gt; subject emits&lt;/p&gt;

&lt;p&gt;and when the &lt;code&gt;unsubscribe$&lt;/code&gt; subject emit? =&amp;gt; When the component is going to destroy since &lt;code&gt;ngOnDestroy&lt;/code&gt; will invoke before it's going to destroy&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Pro?&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The code inside &lt;code&gt;ngOnDestroy&lt;/code&gt; is not going to touch again&lt;/li&gt;
&lt;li&gt;when you subscribe, the declarative code make it explicit you made the unsubscription&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Con?&lt;/strong&gt;&lt;br&gt;
I just think this logic is more complicated to understand. It required some knowledge to read code.&lt;/p&gt;

&lt;p&gt;Actually, there's more ways to unsubscribe, and the reason I only introduce these two is because that the first one is the more simple-to-get one, and the second one the the one I had been seeing a lot in my working code base 😂&lt;/p&gt;




&lt;p&gt;Let me know if you have questions. If you think I am not elaborate enough, feel free to see below links that I gathered for you :)&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://www.notion.so/Why-It-s-Important-to-Unsubscribe-from-RxJS-Subscription-by-Netanel-Basal-Netanel-Basal-948ac83ee9124ab8811f88c6a951573c" rel="noopener noreferrer"&gt;Why It’s Important to Unsubscribe from RxJS Subscription | by Netanel Basal | Netanel Basal&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.notion.so/When-to-Unsubscribe-in-Angular-As-you-probably-know-when-you-subscribe-by-Netanel-Basal-Netane-30317e13657140c38c44ffff13902d97" rel="noopener noreferrer"&gt;When to Unsubscribe in Angular. As you probably know when you subscribe… | by Netanel Basal | Netanel Basal&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://rxjs.dev/api/operators/takeUnti" rel="noopener noreferrer"&gt;RxJS - takeUntil&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://medium.com/angular-in-depth/the-best-way-to-unsubscribe-rxjs-observable-in-the-angular-applications-d8f9aa42f6a0" rel="noopener noreferrer"&gt;The Best Way To Unsubscribe RxJS Observables In The Angular Applications! | by Tomas Trajan | Angular In Depth | Medium&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.learnrxjs.io/learn-rxjs/operators/filtering/takeuntil" rel="noopener noreferrer"&gt;takeUntil - Learn RxJS&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.digitalocean.com/community/tutorials/angular-takeuntil-rxjs-unsubscribe" rel="noopener noreferrer"&gt;Using the takeUntil RxJS Operator to Manage Subscriptions Declaratively | DigitalOcean&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

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