<?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: Zulhariz Merican</title>
    <description>The latest articles on DEV Community by Zulhariz Merican (@zulhariz).</description>
    <link>https://dev.to/zulhariz</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%2F795115%2F1945720e-8dc9-4c6e-bf73-d6f5d06389b8.jpg</url>
      <title>DEV Community: Zulhariz Merican</title>
      <link>https://dev.to/zulhariz</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/zulhariz"/>
    <language>en</language>
    <item>
      <title>Understanding Asynchronous Javascript with Callbacks</title>
      <dc:creator>Zulhariz Merican</dc:creator>
      <pubDate>Sat, 10 Sep 2022 09:06:41 +0000</pubDate>
      <link>https://dev.to/zulhariz/understanding-asynchronous-javascript-with-callbacks-4m86</link>
      <guid>https://dev.to/zulhariz/understanding-asynchronous-javascript-with-callbacks-4m86</guid>
      <description>&lt;p&gt;My first encounter with Asynchronous javascript was definitely difficult for me to get a real grasp on the concepts. So then i started laying out the distinctions between each concepts to get a birds view on the matter at hand.&lt;/p&gt;

&lt;p&gt;To start of, i think there are 3 terms that you must fully understand , &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Functions&lt;/li&gt;
&lt;li&gt;Asynchronous&lt;/li&gt;
&lt;li&gt;Callbacks&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;lets uncover each.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Functions&lt;/strong&gt; - Functions are set of &lt;strong&gt;reusable code&lt;/strong&gt; that will start computing after ** INVOKING**. (In Other words , do this code , when i call you.)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;e.g.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function dugong(){
//some code (reusable code)
}

dugong();//invoking || called , so start doing.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Asynchronous&lt;/strong&gt; - Javascript reads and executes code line by line this is what we call (Synchronous) , and that fine if you just want to execute a code which is just really straightforward but say you have a highly scalable website , it doesnt make sense to just wait for a particular task at hand to complete while the website just stays idle or freezes.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;So to tackle this , asynchronous javascript comes to play.&lt;/p&gt;

&lt;p&gt;Essentially asynchronous javascript is doing something without disrupting the main flow of the environment.Encounters of asynchronous javascript are events, say you 'click' a button then only it starts executing the code within it. (In Other words , do this(code) when i do this (click) )&lt;/p&gt;

&lt;p&gt;e.g.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;btn.addEventListener('click' , ()=&amp;gt;{
//some code
}

//this means after CLICKING then only it will execute.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Callbacks&lt;/strong&gt; - Callbacks are functions that are nested in another function.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//higher order function
function dugong(not_dugong){
//some code
not_dugong(); // callback function
}

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

&lt;/div&gt;



&lt;p&gt;okay now that we've laid the terms , lets start showing an example.To make this interesting lets have a Scenario. &lt;/p&gt;

&lt;p&gt;Scenario - You asked a girl out on a date , you buy her flowers  you take her to the movies , you buy her dinner and then you confessed to her that you love her , she being unsure with her feelings she asked you to give her time to think about it. But because you are not a simp and not to wait on her , you asked anna and donna out while she still thinks about making a decision. (okay , lets start coding this silly story ..... player !)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//here we have a function structure , lets take the first function example the " function lets_date(callback)" , the First Content of this function is that it will execute the "console.log('lets go on a date')" 

function lets_date(callback){
    console.log('lets go on a date'); 
    callback(); // Moving on to the second line it finds that we INVOKED a function with a name of callback(); but there is no code content in the callback function , so it looks to the parameter of the main function (lets_date (parameter) ) and we can start constructing a batch of different code to execute there and so forth and if you want to call//invoke a set of other functions like in this case the "flowers() function" we include it in the callback function for it to be executed.
    } 

    function flowers(callback){
    console.log('buy flowers');
    callback();
    }

    function movies(callback){
    console.log('go to the movies');
    callback();
    }

    function dinner(callback){
    console.log('im broke but going to pay you for dinner');
    callback();
    }

    function confess(callback){
        console.log('You : i love you');
        console.log('Her : give me sometime to think about it')
        callback();
    }

    function not_a_simp(callback){
        console.log('i am not simp');
        callback();
    }

    function others(callback){
        console.log('ask anna out');
        callback();
    }
    function the_others(){
        console.log('ask dinnie out');
    }

    function love(){
        lets_date((callback)=&amp;gt;{ // you can just leave it an empty bracket also its fine , but im just trying to point out that this is the callback function and we state that in this callback function we have declared//invoked//called "flowers function" 
            flowers((callback)=&amp;gt;{
                movies((callback)=&amp;gt;{
                    dinner((callback)=&amp;gt;{
                        confess((callback)=&amp;gt;{
                            not_a_simp((callback)=&amp;gt;{ // in this function we have a callback which have an async function which is SetTimeOut
                                setTimeout((callback)=&amp;gt;{ // here would be asynchronous
                                    const yes = ()=&amp;gt;{ return console.log('One eternity later , Her : i love you too bro');  
                            }

                            if(yes){
                                yes();
                                console.log('You : i love you too back');
                                console.log('You: lets get married')
                            }else {
                                console.log('She rejected you , You : fcuk you');
                            }

                                } , 15000)// here would be asynchronous

                                others((callback)=&amp;gt;{
                                    the_others();
                                })
                            })
                        })
                    })
                })
            })
        })
    }

    love();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But this is not an ideal way , because then you'll have nested functions on top of nested functions which can be daunting ,  we call this ( callback hell ). This is where we can use Promises or async await to make our code more readable.&lt;/p&gt;

&lt;p&gt;I hope this was insightful, understanding this concept really helps me and i hope it does for you too. If you need more clarification on this do let me know in the comments. I'll set up a new post regarding Promises and async in the future.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>callbacks</category>
      <category>async</category>
    </item>
    <item>
      <title>First post</title>
      <dc:creator>Zulhariz Merican</dc:creator>
      <pubDate>Wed, 19 Jan 2022 14:05:53 +0000</pubDate>
      <link>https://dev.to/zulhariz/first-post-59ni</link>
      <guid>https://dev.to/zulhariz/first-post-59ni</guid>
      <description>&lt;p&gt;This is my first time posting this , hopefully to look back years from now and to see how far i have come. &lt;/p&gt;

</description>
      <category>tests</category>
      <category>testing</category>
      <category>softwaredevelopment</category>
    </item>
  </channel>
</rss>
