<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Ridha Mezrigui  on Medium]]></title>
        <description><![CDATA[Stories by Ridha Mezrigui  on Medium]]></description>
        <link>https://medium.com/@ridhamezrigui07?source=rss-8d1e8c2d4370------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*qijUokfVkvHDgNfbW7yTbA.jpeg</url>
            <title>Stories by Ridha Mezrigui  on Medium</title>
            <link>https://medium.com/@ridhamezrigui07?source=rss-8d1e8c2d4370------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 08:50:59 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@ridhamezrigui07/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[JavaScript: bind(), call() and apply()]]></title>
            <link>https://medium.com/@ridhamezrigui07/javascript-bind-call-and-apply-100edfd86d83?source=rss-8d1e8c2d4370------2</link>
            <guid isPermaLink="false">https://medium.com/p/100edfd86d83</guid>
            <dc:creator><![CDATA[Ridha Mezrigui ]]></dc:creator>
            <pubDate>Sun, 22 Jan 2023 22:28:33 GMT</pubDate>
            <atom:updated>2023-01-24T09:46:20.818Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ipiWNtrdYCnu8lN4oM-EVw.jpeg" /></figure><h3>Introduction</h3><p>Hello, community 🙌🏻,</p><p>JavaScript offers various methods for invoking a function, including <strong><em>bind()</em></strong>, <strong><em>call()</em></strong>, and <strong><em>apply()</em></strong>.</p><p>These methods are utilized to establish the value of <strong><em>this </em></strong>within a function and are particularly useful when working with objects and constructors.</p><p>This blog post will take a closer look at each of these methods, how they work, and when to use them.</p><h3>1. bind()</h3><p>The <strong><em>bind()</em></strong> method creates a new function with a specific value for this. It does not call the function, it just creates a new function with a permanently bound the <strong><em>this </em></strong>value. To use bind(), you pass in the value you want this to reference, and it returns a new function with the <strong><em>this </em></strong>value set to the value you provided.</p><pre>let obj = {<br>  name: &#39;mz&#39;,<br>}<br><br>function sayName () {<br>    console.log(this.name)<br>  }<br><br>let boundSayName = sayName.bind(obj)<br>boundSayName() // Output: mz</pre><p>In this example, we use bind()to create a new function boundSayName() that has <strong><em>this</em></strong> set to obj. This means that when we call boundSayName(), this.name will be equal to mz</p><h3>2. call()</h3><p>The <strong><em>call()</em></strong> method is used to call a function with a specific value for <strong><em>this</em></strong>. It also allows you to pass in arguments one by one.</p><p>To use <strong><em>call()</em></strong>, you pass in the value you want <strong><em>this </em></strong>to reference and the arguments for the function.</p><pre>let obj1 = { name: &#39;mz&#39; }<br>let obj2 = { name: &#39;Bob&#39; }<br><br>function sayName(greeting){<br>  if(greeting)<br>  console.log(`${greeting}  ${this.name}`)<br>  else console.log(this.name)<br>}<br><br>sayName.call(obj1)        // Output: mz<br>sayName.call(obj2, &#39;Hi&#39;) // Output: Hi Bob </pre><p>In the first example, the function is invoked by calling sayName.call(obj1). This sets the value of <strong><em>this </em></strong>within the function to obj1, so this.name is equal to mz and the output is mz.</p><p>In the second example, the function is invoked by calling sayName.call(obj2, &#39;Hi&#39;). This sets the value of this within the function to obj2, so this.name is equal to Bob and the output is Hi Bob.</p><p>In this way, call() method allows us to change the context of a function, and to use the same function with different objects, and with that, we can reuse the function on different objects with different values.</p><h3>3. apply()</h3><p>The <strong><em>apply()</em></strong> method is similar to the <strong>call() </strong>method, but it allows you to pass in an array of arguments.</p><p>To use <strong><em>apply()</em></strong>, you pass in the value you want <strong><em>this </em></strong>to reference and an array of arguments for the function.</p><pre>let numbers = [1, 2, 3]<br>console.log(Math.max.apply(null, numbers)) // Output: 3</pre><p>In this example, we are using the apply() method to call the Math.max function and pass in an array of numbers as the arguments.</p><p>The first argument is null because we don’t need to set the value of <strong><em>this</em></strong>.</p><h3>Summary</h3><p>In summary, <strong><em>bind()</em></strong>,<strong><em> call()</em></strong>, and <strong><em>apply()</em></strong> are powerful tools in JavaScript that allow you to set the value of <strong><em>this </em></strong>inside a function.</p><p><strong><em>Bind()</em></strong> creates a new function with a permanently bound the <strong><em>this </em></strong>value.</p><p>C<strong><em>all()</em></strong> is used to call a function with a specific value for <strong><em>this</em></strong> and pass in arguments one by one.</p><p><strong><em>Apply()</em></strong> is used to call a function with a specific value for <strong><em>this </em></strong>and pass in an array of arguments.</p><p>Knowing when to use each of these methods can help you write more efficient and maintainable code.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=100edfd86d83" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Asynchronous JavaScript: Promises and async-await]]></title>
            <link>https://medium.com/@ridhamezrigui07/asynchronous-javascript-promises-and-async-await-570d1f1354dc?source=rss-8d1e8c2d4370------2</link>
            <guid isPermaLink="false">https://medium.com/p/570d1f1354dc</guid>
            <category><![CDATA[asynchronous]]></category>
            <category><![CDATA[promises]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[asyncawait]]></category>
            <dc:creator><![CDATA[Ridha Mezrigui ]]></dc:creator>
            <pubDate>Sun, 12 Dec 2021 00:22:28 GMT</pubDate>
            <atom:updated>2021-12-12T16:52:15.702Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FXTfSX3Xt8GruYQXTBJrMw.jpeg" /><figcaption>All about promises and async await🧡</figcaption></figure><p>Hello, community 🙌🏻,</p><p>As a JavaScript developer, knowing how to deal with asynchronous operations is an essential skill.</p><h4><strong><em>1. What are Promises </em></strong>🤔<strong><em> ?</em></strong></h4><p>Promises are a new feature introduced by ECMAScript 2015 (ES6) that is used to get the result of an asynchronous JavaScript operation like callbacks but in a fancy way.</p><p>Technically, a JavaScript promise is an object that returns a value that you hope to receive in the future.</p><h4>2. Promise declaration syntax 🦄</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/821/1*vA-P9WHwHa1UeWMQx5L7LQ.png" /><figcaption>Promise declaration syntax</figcaption></figure><p>Using the Promise Class we can make a promise object that takes a callback function as an argument and this function itself takes two arguments resolve and reject they are two callback functions.</p><p><strong>resolve: </strong>get executed when the operation is done with success<strong>.</strong></p><p><strong>reject</strong>: get executed when the operation failed.</p><h4>3. Simulation of real call API using Promises ⏳</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uQ6iPD2d_gdU5la5tT-Qag.png" /><figcaption>Call API simulation</figcaption></figure><p>In the example, above we implemented a simple call API simulation using the facade function<strong> setTimeout.</strong></p><p>getUserById is a function that takes id as an argument and returns the user if exists, otherwise returns false.</p><p>Depending on the result of the getUserById function, if the user exists, we execute the resolve function and pass the user data as an argument or execute the reject function and pass an error message as an argument.</p><h4>4. Promise object status 📉</h4><p>The promise object has 3 different status depending on the asynchronous operation.</p><p><strong>Pending </strong>if the asynchronous operation is still not yet done.</p><p><strong>resolved </strong>if the asynchronous operation was done successfully.</p><p><strong>rejected </strong>if the asynchronous operation failed.</p><h4>5. Getting the Promise result ✅</h4><p>Getting the result of a Promise using <strong>.then .catch </strong>syntax.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JUU_V0wbVqoahzBCMNo9yw.png" /><figcaption>.then .catch syntax</figcaption></figure><p>The promise class provides two methods, then used to inject a function when the status is resolved and catch to inject a function when the status is rejected.</p><p>Besides that, the Promise class provides a third method called finally that allows us to run a function in both cases resolved or rejected.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/807/1*r5ZaspShWSnpVY52GAd3_Q.png" /><figcaption>Finally example</figcaption></figure><h4>6. Promise Chaining ⛓️</h4><p>As we know that every promise object has 3 methods (then, catch, finally) the thing is that every method returns a new promise.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/832/1*XfGZVT_CjqdOzv8uy60oNQ.png" /><figcaption>Promise Chaining Example</figcaption></figure><p>In the example above :</p><ol><li>We take the result of the promise with the first method then and return a new promise that resolves a new value: res + 1 (1 + 1)</li><li>In the second then method, we accept the result of the promise that returns from the first then and returns a new promise by resolving a new value res × 2 (2 × 2)</li><li>The final result was printed using the third, then method.</li></ol><p><strong><em>Promise Changing </em></strong>is a powerful and clean approach used to manipulate the result of a promise on multiple levels. (it is the same for catch and finally)</p><h4>7. Promise.all() and Promise.race() ⚙️</h4><ol><li>Promise.all() : static method takes an array of Promises as an argument and returns a new Promise that resolves the resolved values from all Promises that exists in the array, but if one of these promises is rejected, then the Promise returned is also rejected with an error from the first rejected promise.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2iaRFJYVH9wRxWg6vEZnxw.png" /><figcaption>Promise.all() resolve example</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1011/1*51hhmz2r9bt5QOxAUQPXag.png" /><figcaption>Promise.all() reject example</figcaption></figure><p>2. Promise.race() : static method takes an array of Promises as an argument and returns a new Promise depending on the first Promise that resolves or rejects from all Promises exists in the array and <strong>first<em> </em></strong>means first in time not first in order.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YPWvI4GxV2yMdb4mmdTvvA.png" /><figcaption>Promise.race() example</figcaption></figure><h4>8. async-await 💎</h4><p>async-await is a JavaScript feature introduced by ECMAScript 2016 (ES7) in the order to give us more options to deal with asynchronous operations.</p><p>async-await built on top of Promises so we can use it to get the result of a promise object also it gives us more clean code and more readable code.</p><p>To see the beauty of async-await let’s first refactor our getUserById function</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QkcHwBiVCILXzeBKUkuEuQ.png" /><figcaption>Refactor getUserById</figcaption></figure><p>now let’s see how to get the result of this function using async-await syntax</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/905/1*TFDJrrfhuJ7_812961rLNA.png" /><figcaption>async-await syntax</figcaption></figure><p>With this approach, we can just get the resolved value cause if the promise rejects we will get nothing so how to handle errors when we use async-await?</p><p>The answer is JavaScript try-catch :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*oBhH1_zmAp98Yp-FG1PEvg.png" /><figcaption>Catch error using try-catch</figcaption></figure><p>try-catch is awesome but repeating it with every call of an asynchronous function is a little bit annoying, that is why we need to write a generic function that can handle any other asynchronous function.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/665/1*AytjiNRMlLV5EfcWTVYG3A.png" /><figcaption>handleAsync function</figcaption></figure><p>handleAsync takes an asynchronous callback function and returns an array containing the result and the error.</p><p>Using JavaScript destructuring, we can get the values of the result and the error.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YPRj4NV2JaGf9JtW9VuF9g.png" /><figcaption>Using handleAsync</figcaption></figure><p>we used getResult function cause in JavaScript await is allowed to use only in an async function but if you don’t like to use getResult() you can use an IIFE function (Immediately Invoked Function Expression).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XnMGfOy_0oBCx9wcCUZRag.png" /><figcaption>Using handleAsync in an IIFE function</figcaption></figure><h4>9. Asynchronous operations inside loops 💪🏻</h4><p>By default, JavaScript loops don’t support asynchronous operations but there is a way to do asynchronous operation inside a loop.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yeAr8mVHu_CpPEn5w77Bzg.png" /><figcaption>asynchronous with map</figcaption></figure><p>We can do asynchronous operations inside a map by wrapping it with <strong>await Promise.all()</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=570d1f1354dc" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How does javascript work in the background (part 2)]]></title>
            <link>https://medium.com/@ridhamezrigui07/how-does-javascript-work-in-the-background-part-2-e290cf2e79c6?source=rss-8d1e8c2d4370------2</link>
            <guid isPermaLink="false">https://medium.com/p/e290cf2e79c6</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[scopes]]></category>
            <category><![CDATA[higher-order-function]]></category>
            <category><![CDATA[closure]]></category>
            <dc:creator><![CDATA[Ridha Mezrigui ]]></dc:creator>
            <pubDate>Sat, 30 Oct 2021 16:55:21 GMT</pubDate>
            <atom:updated>2022-01-07T22:46:44.005Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TqP76hI2JVY6hUWAwKYSwA.png" /><figcaption>All about closure 💛</figcaption></figure><p>Hello community 🙌🏻,</p><p>This is part 2 of“ <strong>How does javascript work in the background” </strong>blog series 📒.</p><p>If you didn’t see part 1 you can take a look from here 👀</p><p><a href="https://medium.com/@ridhamezrigui07/how-does-javascript-work-in-the-background-part-1-2a5de292cc23">How does javascript work in the background (Part 1)</a></p><p>Today we will talk about <strong><em>Global memory, Local memory, Scope &amp; Variable Scope, Higher Order functions</em></strong> and one of the most important and powerful javascript concept it is<strong><em> closure.</em></strong></p><h4><strong><em>1. Global Memory </em></strong>🌎</h4><p>Global memory is where all global variables are stored, it is connected to the global execution context. Every variable or function declared in the global execution context will be stored in the global memory.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jnns8Op-Ew1Vwl-HZGD6CA.png" /><figcaption>Global memory</figcaption></figure><p>In the first reading, javascript will put all variables and functions declaration in the global memory as <strong>key-value pairs,</strong> the variable count will take the initial value 0 and the function will take the code definition as a value.</p><h4><strong><em>2. Local Memory </em></strong>🗺️</h4><p>Every javascript function has own local memory used to store local variables.</p><p>When a function tries to get variable value it looks first in its local memory if the variable doesn’t exist then the function will be searching for it in the global memory.</p><h4><strong><em>3. Scope &amp; Variable Scope </em></strong>🔭</h4><p>The scope is a mechanism used by the language in order to handle the visibility and the access of variables.</p><p>There are many types of scope like <strong>lexical scope, dynamic scope…</strong> however javascript use the <strong>lexical scope </strong>called also<strong> static scope</strong>.</p><p>The main idea of lexical scope is that child’s function can access any variable declared in the parent context. We can say that functions are lexically bound to the execution context of their parents, but the parent can’t access any variable declared in a child context.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dQpdQVw7HtFZCOGSChbZWA.png" /><figcaption>Scope example</figcaption></figure><p>In the example above the function calculate can access to <strong>a </strong>and <strong>b </strong>but if we try to access the variable sum outside calculate we will get an error ❌ <strong><em>‘ReferenceError: sum is not defined’ </em></strong>and the same thing if we try to access <strong>b </strong>outside printSomme.</p><p>Another thing you need to know about javascript scope is that inside the same function if there is a variable declared with let or const keyword inside a block {} can’t be accessed outside this block it is called block scope however a variable declared with the keyword var can’t have the block scope.</p><h4><strong><em>4. Higher-Order functions </em></strong>⚡</h4><p>the basic definition is that any function takes another function as an argument or/and returns a function as result called <strong>‘Higher Order function’.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ElEu_zXOI1Z4LEKGR6OXsA.png" /><figcaption>Higher Order Function Simple Example</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VsZve7CCKRhEmGy7cQlNKA.png" /><figcaption>Higher Order Function Advanced Example</figcaption></figure><h4><strong><em>5. Closure </em></strong>🏆</h4><p>Closure is when a function remembers the variables around it, even when that function is executed in a different context.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*A91lOth6WG3j6QWNe5u7WA.png" /><figcaption>getSum execution context</figcaption></figure><p>In the example above when the getSum function gets invoked in line 13 javascript will push the function into the call stack and a brain new execution context will be created a and b will be stored in the local memory as<strong> key-value pairs.</strong></p><p>When the execution of getSum() is done, the brain new execution context and the local memory will be deleted, and the function will be removed from the call stack and the result of that function will be stored in the calc variable.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*lLg0A-M7G70DZ5PKbtA2BA.png" /><figcaption>End of getSum execution context</figcaption></figure><p>Now calc holds the calculate function that returns the sum of a and b but these two variables don’t exist either in the global memory, neither in the local memory of the calculate function, and the getSum function is deleted with its local memory. The question is how the calculate function will get the value of these two variables a and b?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*J6ZMNcH_2HOfM8aViXB_fQ.png" /><figcaption>calc execution context</figcaption></figure><p>To answer this question we need to know that every function returned, as a result, will take all variables that it needs when will get invoked, these variables are stored in small memory attached to the function called <strong>backpack</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YLLXwFtkOZd3pkkwAUh7Xg.png" /><figcaption>function backpack</figcaption></figure><p>So the idea is to get any variable value the function checks the <strong>backpack</strong> first if this variable doesn’t exist in the <strong>backpack</strong> the function will check the local memory if also doesn’t exist then the function will look at the global memory.</p><p>We call all this process<strong> Closure.</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e290cf2e79c6" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How does javascript work in the background (Part 1)]]></title>
            <link>https://medium.com/@ridhamezrigui07/how-does-javascript-work-in-the-background-part-1-2a5de292cc23?source=rss-8d1e8c2d4370------2</link>
            <guid isPermaLink="false">https://medium.com/p/2a5de292cc23</guid>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Ridha Mezrigui ]]></dc:creator>
            <pubDate>Tue, 12 Oct 2021 20:58:05 GMT</pubDate>
            <atom:updated>2022-01-07T22:48:01.039Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OD84r_1QsdR87tnamFGkkQ.png" /><figcaption>javascript ❤️</figcaption></figure><p>Hello, community 🙌🏻,</p><p>Right now javascript is one of the most popular programming languages in the world.</p><p>In this blog, I will share the most important concepts about javascript that you have to know as a web developer.</p><h4><strong>1. Thread of execution and Global execution context </strong>💡</h4><p>As we know javascript is a single thread language this is mean that every javascript code executes line by line using <strong>Thread of execution</strong>, and this process happens inside a container called the <strong>global execution context.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tIgcKUWZZBuwgz26RumqIw.png" /></figure><p>But this global execution context can contain lines of code and also functions, so how can javascript handle this situation?</p><p>The answer is that every function in javascript has its own execution context, when a function gets invoked javascript will create a <strong><em>brain new execution context </em></strong>then the thread of execution will switch from the global execution context to the brain new execution context and starts running function’s code line by line, Javascript manages all the execution contexts with something called <strong>Call Stack.</strong></p><h4><strong>2. Call Stack</strong> (Last In First Out) 🧮</h4><p>By default, the call stack contains the global execution context, when a function gets invoked javascript push it inside the call stack so the thread of execution switch from global to this new execution context, and when the function has done javascript will remove it from the call stack then the thread of execution go back to the global.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fcR7gVqrlyQR79KIaPrQmg.png" /></figure><p>In this example when the thread of execution comes to line 7 javascript will figure out that this is not a simple line of code, but it is a function call, so it will push this function into the call stack and create a brain new execution context so the thread of execution switch from the global to this new execution context and go throw the code of <strong>SayHello() </strong>line by line.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6_FfKafS6Y_hcmS82vHUYw.png" /></figure><h4><strong>3. CallBacks, CallBack Queue and Event Loop </strong>➿</h4><p>Javascript is a <strong><em>single thread language </em></strong>it can’t handle many operations in the same time but what makes it very powerful is its <strong><em>asynchronous nature</em></strong> this is mean that javascript when executing an asynchronous operation <strong><em>didn’t wait for the result </em></strong>of this operation because waiting the result of an asynchronous code will make it a slow language (example get data from database). So the idea is to run any asynchronous code without waiting for the result and jump to the next line when the result is ready javascript gets it using callBack functions. To simplify, a <strong><em>callback function </em></strong>is a normal function gets executed when an asynchronous operation is done in order to get or manipulate the result.</p><p>But the question now is where javascript stores these callback functions and how can it manage their order?. The answer is <strong><em>CallBack Queue</em></strong> basically it is a simple queue (first in last out) used just to store callback functions until the asynchronous operation is done.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pPqtpq35i_cRlOP8ymQrAQ.png" /></figure><p>In this example, we have an asynchronous function called getUserDataFromApi this function will take 1 second to get the data I used setTimout function to simulate the real HTTP request, to get the result I passed a callBack function called ShowUserData takes userdata as an argument and console log it.</p><p>Let’s what will happen when the thread of execution runs the function getUserdataFromApi() at line 13 :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fDSpydUdQ0s6i6pV_DwhNg.png" /></figure><p>When the thread arrives at line 8 getUserdataFromApi() will be pushed into the Call Stack, a brain new execution context will be created, and the callBack function ShowuserData will be pushed into<strong><em> CallBack Queue.</em></strong></p><p>javascript will not wait 1 sec for the result, but it will remove getUserdataFromApi() from the call stack and go back to global to execute the next line and console log the number 2.</p><p>After 1sec the result will be done and the call stack will be empty then javascript will remove ShowUserData() and push it into the call stack and a brain new execution context will be created.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Mb-if5l7kF-APMHLajtU5A.png" /></figure><p>There is an important information you need to know that any function in the <strong><em>callback queue </em></strong>will be pushed into the<strong> call stack</strong> just when this call stack is empty, but the question is how javascript will know if the stack is empty or not?</p><p>Here we introduce the <strong>Event Loop, </strong>which is responsible for checking if the stack is empty or not. If the call stack is not empty even after 2 sec, the ShowUserData will wait for more time.</p><p>The output of our example will be :</p><p>1</p><p>2</p><p>user data</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2a5de292cc23" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>