<?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: Pan Wangperawong</title>
    <description>The latest articles on DEV Community by Pan Wangperawong (@pan).</description>
    <link>https://dev.to/pan</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%2F154321%2F4cef93e2-8ace-429a-ac49-b999402e8d1c.jpg</url>
      <title>DEV Community: Pan Wangperawong</title>
      <link>https://dev.to/pan</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/pan"/>
    <language>en</language>
    <item>
      <title>Integrate the Reminders API with Your Alexa Skill to Deepen Customer Engagement</title>
      <dc:creator>Pan Wangperawong</dc:creator>
      <pubDate>Fri, 06 Sep 2019 18:01:40 +0000</pubDate>
      <link>https://dev.to/pan/integrate-the-reminders-api-with-your-alexa-skill-to-deepen-customer-engagement-208c</link>
      <guid>https://dev.to/pan/integrate-the-reminders-api-with-your-alexa-skill-to-deepen-customer-engagement-208c</guid>
      <description>&lt;p&gt;Integrating the Reminders API in your skill is a great way to actively extend its utility without requiring customers to launch your skill. With the Reminders API, you can engage more frequently with your customers and become a part of their routines.&lt;/p&gt;

&lt;center&gt;&lt;h2&gt;Learn more by clicking below&lt;br&gt;👇&lt;/h2&gt;&lt;/center&gt;

&lt;p&gt;&lt;a href="https://developer.amazon.com/blogs/alexa/post/2b4de691-9cad-4c82-86e0-98e674786742/integrate-the-reminders-api-with-your-skill-to-deepen-customer-engagement1" rel="noopener noreferrer"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fetabaqfuvt8945pbevja.png" alt="Click to see more"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;If you'd like updates on more content like this, follow me on Twitter &lt;strong&gt;&lt;a href="https://twitter.com/itspanw" rel="noopener noreferrer"&gt;@ItsPanW&lt;/a&gt;&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;

</description>
      <category>alexa</category>
      <category>beginners</category>
      <category>tutorial</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Artificial Artificial Intelligence Meat🥩API and How We Are Eating It Up</title>
      <dc:creator>Pan Wangperawong</dc:creator>
      <pubDate>Mon, 06 May 2019 14:45:19 +0000</pubDate>
      <link>https://dev.to/pan/artificial-artificial-intelligence-meat-api-and-how-we-are-eating-it-up-85p</link>
      <guid>https://dev.to/pan/artificial-artificial-intelligence-meat-api-and-how-we-are-eating-it-up-85p</guid>
      <description>&lt;p&gt;Having co-founded a machine learning company, I’ve seen some AMAZING things, but not necessarily high-tech. &lt;/p&gt;

&lt;p&gt;Advancements in machine learning (ML) and artificial intelligence (AI) are happening at a faster rate than ever, but not fast enough. Data is still a limiting factor and that's where humans 🙋‍♂️🙋‍♀️ come into play. Even more impressive than ML and AI, we have Artificial Artificial Intelligence (AAI), AKA Meat🥩APIs. &lt;/p&gt;

&lt;p&gt;The Meat API uses humans as "artificial intelligence" as they collect the necessary amount of data to actually train an ML model. An extreme case of fake it till you make it 🤷‍♂️. I've commonly seen "AI" companies bootstrap their company with the Meat API. Take an AI assistant service that can help reply to emails and schedule meetings. To the end user, everything seems to be done with human quality and even has human-like grammatical issues. Little did the user know, the emails were in fact written by humans 😂. We are totally eating up the meat.&lt;/p&gt;

&lt;p&gt;I hope you enjoyed my satirical take on "Artificial Artificial Intelligence". But really -- people just want to believe. Companies that do machine learning still rely heavily on human intervention to label and edit data that ML models train on and we are still far from being in West World.&lt;/p&gt;

&lt;p&gt;For more content like this follow me on Twitter &lt;a href="https://twitter.com/itspanw"&gt;@itspanw&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>machinelearning</category>
      <category>ai</category>
      <category>joke</category>
    </item>
    <item>
      <title>Intro to Alexa Skill Development</title>
      <dc:creator>Pan Wangperawong</dc:creator>
      <pubDate>Sat, 04 May 2019 19:06:33 +0000</pubDate>
      <link>https://dev.to/pan/intro-to-alexa-skill-development-for-free-1dgf</link>
      <guid>https://dev.to/pan/intro-to-alexa-skill-development-for-free-1dgf</guid>
      <description>&lt;p&gt;If you ever wanted to build your first Alexa Skill, checkout my tutorial on the Amazon Developer Forum. It shows how you can get started for free. Happy skill development!&lt;/p&gt;


&lt;center&gt;&lt;h2&gt;Click Below 👇&lt;/h2&gt;&lt;/center&gt;

&lt;p&gt;&lt;a href="https://forums.developer.amazon.com/content/kbentry/204223/build-your-first-alexa-skill-as-an-alexa-hosted-sk.html"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--kK_D31RR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/9fs8uu8vcv10l2iyehl9.png" alt="Intro to Alexa Skill Development tutorial"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;If you'd like updates on more content like this, follow me on Twitter &lt;strong&gt;&lt;a href="https://twitter.com/itspanw"&gt;@itspanw&lt;/a&gt;&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;

</description>
      <category>alexa</category>
      <category>beginners</category>
      <category>javascript</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>What Does Good Code Look Like 👀?</title>
      <dc:creator>Pan Wangperawong</dc:creator>
      <pubDate>Mon, 22 Apr 2019 16:01:46 +0000</pubDate>
      <link>https://dev.to/pan/what-does-good-code-look-like-1he3</link>
      <guid>https://dev.to/pan/what-does-good-code-look-like-1he3</guid>
      <description>&lt;p&gt;When I was an instructor, I would often receive questions about how to know what does good code looks like. In my opinion, programming is a highly creative art form so there are multiple ways to write good code. Quantifying good code will often be dependent on the people you work with and the practices your team follows.&lt;/p&gt;

&lt;p&gt;In general, I believe in good programming philosophies rather than dogmatic ways to write good code.&lt;/p&gt;

&lt;p&gt;Here are some philosophies I like to keep in mind -- &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Art is expression with an intent&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  What is code?
&lt;/h2&gt;

&lt;p&gt;Code expresses an intention to two audiences -- (1) a fellow developer 👩‍💻👨‍💻 and (2) a computer 🤖💻. It is about communication and can take on different forms in different programming languages. It can also take on different forms between different audiences!&lt;/p&gt;

&lt;h2&gt;
  
  
  Top Priority 👩‍💻👨‍💻
&lt;/h2&gt;

&lt;p&gt;A primary priority in writing code is communicating clearly to your fellow developers about what your code does and how the different parts fit together. This is essential, as we usually develop software in collaboration with other developers. If others cannot understand your code, then they are unable to help contribute. This hinders creativity and productivity in building something awesome.&lt;/p&gt;

&lt;h2&gt;
  
  
  Secondary Priority 🤖💻
&lt;/h2&gt;

&lt;p&gt;A secondary priority is writing code to effectively communicate to a computer. This means you are writing code that is working as intended and is efficient. Optimal performance is necessary for good user experience because no one likes slow applications. However, optimally-performant code is not always readable, so there needs to be a trade-off here. As a developer, you may have to find creative ways to optimize your code without making your code more difficult to read. Using other tools such as load balancing, caching, or CDNs might help with this.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;I'm sorry if I didn't tell you how to write "good code" as it is something that needs to be defined with and tuned to the people you work with. You'll find that from working with many different teams, you will run into different programming practices and styles. The most important thing is to write code that communicates well...to people!&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>tutorial</category>
      <category>gettingstarted</category>
      <category>programming</category>
    </item>
    <item>
      <title>Async-Await vs. Then to Avoid Callback Hell 📞😈</title>
      <dc:creator>Pan Wangperawong</dc:creator>
      <pubDate>Fri, 19 Apr 2019 06:24:45 +0000</pubDate>
      <link>https://dev.to/pan/when-to-use-async-await-vs-then-with-promises-1gb7</link>
      <guid>https://dev.to/pan/when-to-use-async-await-vs-then-with-promises-1gb7</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr9j3oh5blu08gffgll3d.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr9j3oh5blu08gffgll3d.jpg" alt="Dante's Inferno Callback Hell" width="800" height="433"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Callback Hell 🔥
&lt;/h2&gt;

&lt;p&gt;When working with JavaScript, there is a concept known as callback hell. It describes a deeply nested set of callback functions that is hard to read and maintain. Callback hell happens due to the asynchronous non-blocking nature of JavaScript. Below is an illustration based on Dante's 😈 nine circles of hell.&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="nf"&gt;hell&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;firstCircle&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;secondCircle&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nf"&gt;thirdCircle&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="nf"&gt;fourthCircle&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                    &lt;span class="nf"&gt;fifthCircle&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                        &lt;span class="nf"&gt;sixthCircle&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                            &lt;span class="nf"&gt;seventhCircle&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                                &lt;span class="nf"&gt;eighthCircle&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                                    &lt;span class="nf"&gt;ninthCircle&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                                        &lt;span class="nf"&gt;alert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hell has Frozen Over!&lt;/span&gt;&lt;span class="dl"&gt;"&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="p"&gt;}&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="p"&gt;}&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;h2&gt;
  
  
  Promises
&lt;/h2&gt;

&lt;p&gt;You commonly encounter callback hell when making an AJAX HTTP request. To flatten out nested callbacks for readability and maintainability, &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" rel="noopener noreferrer"&gt;Promises&lt;/a&gt; can be used. With Promises, there are two techniques for flattening out our callbacks -- (1) &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/then" rel="noopener noreferrer"&gt;&lt;code&gt;then&lt;/code&gt;&lt;/a&gt; and (2) &lt;a href="https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Asynchronous/Async_await" rel="noopener noreferrer"&gt;&lt;code&gt;async-await&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;then&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;This pattern helps flatten out nested callbacks into sequential &lt;code&gt;thens&lt;/code&gt;. The entire execution of this &lt;code&gt;fetch&lt;/code&gt; request is completely asynchronous and non-blocking.&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="nf"&gt;someFunc&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;https://someurl.com&lt;/span&gt;&lt;span class="dl"&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;then&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;response&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;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
        &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&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="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;data&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="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;I will not be blocked&lt;/span&gt;&lt;span class="dl"&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;h3&gt;
  
  
  &lt;code&gt;async-await&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;This pattern does the same thing, but is different because each line with &lt;code&gt;await&lt;/code&gt; causes the code execution to block while waiting for the promise to &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/resolve" rel="noopener noreferrer"&gt;resolve&lt;/a&gt;.&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="k"&gt;async&lt;/span&gt; &lt;span class="nf"&gt;someFunc&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;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;https://someurl.com&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
           &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&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="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;I will be blocked until I get a response&lt;/span&gt;&lt;span class="dl"&gt;"&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="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;code&gt;async-await&lt;/code&gt; vs &lt;code&gt;then&lt;/code&gt;
&lt;/h2&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;async-await&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Useful to use if your code works with &lt;code&gt;Promises&lt;/code&gt; and needs to execute sequentially. Due to blocking, you might lose some ability to process code in parallel. I've primarily used &lt;code&gt;async-await&lt;/code&gt; when making API requests from a Node.js server.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;then&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;This has been most useful for me when working on the client so the UI thread is not blocked since requests are being processed in parallel. If you develop your frontend with React.js, a typical use case might be to display a loading screen until a &lt;code&gt;fetch&lt;/code&gt; request returns and then using a &lt;code&gt;setState&lt;/code&gt; to update the UI.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Both &lt;code&gt;Promise&lt;/code&gt; mechanisms can be used to solve the callback hell issue, each with their own optimal use cases. Don't limit yourself to any dogma. Do what makes sense for your use case.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If you found this content useful and would like to get updates on new content, follow me on Twitter &lt;a href="https://twitter.com/itspanw" rel="noopener noreferrer"&gt;@itspanw&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>javascript</category>
      <category>promises</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>What New JavaScript Developers Need to Master 🥋</title>
      <dc:creator>Pan Wangperawong</dc:creator>
      <pubDate>Wed, 17 Apr 2019 15:58:51 +0000</pubDate>
      <link>https://dev.to/pan/what-new-javascript-developers-need-to-master-5dbe</link>
      <guid>https://dev.to/pan/what-new-javascript-developers-need-to-master-5dbe</guid>
      <description>&lt;p&gt;Developers new to JavaScript need to master (1) scope, (2) closure and (3) asynchronous functions. If these three concepts are misunderstood, it can lead to unexpected behavior and unstable code. Developers I have mentored or worked with coming from developing C++ or from no programming experience have all lost productivity to these topics. Whether you are writing spaghetti jQuery code, frontends built with React.js, or Node.js servers, these are helpful starting points for understanding JavaScript. &lt;/p&gt;

&lt;p&gt;Below, I'll go over these concepts with some examples that I hope will clear things up.&lt;/p&gt;

&lt;h2&gt;
  
  
  Closure and Scope
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. Closures
&lt;/h3&gt;

&lt;p&gt;In JavaScript, a closure is created by declaring a function. It is a way to encapsulate data and logic with a specific responsibility.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;jsClosure&lt;/span&gt; &lt;span class="o"&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="c1"&gt;// Everything in here is inside a closure&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;or&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;jsClosure&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// Everything in here is inside a closure&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  2. Scope
&lt;/h3&gt;

&lt;p&gt;When a closure is created, it also comes with an accompanying scope. Through scopes, variables specific to the function (closure) are encapsulated. There are two types of scope -- (a) global which is accessible everywhere in the code and (b) local which is only accessible within a specific closure.&lt;/p&gt;

&lt;p&gt;Common errors arise when some variable is being accessed in the wrong scope, resulting in an undefined variable error.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="cm"&gt;/* 
  Open a JavaScript console and execute each part of the code below in 
  your browser JavaScript console to see it in action (On Windows and 
  Linux: Ctrl + Shift + J. On Mac: Cmd + Option + J).
*/&lt;/span&gt;

&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;globalVariable&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hi, I'm global and could be accessed anywhere&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;jsClosure&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;localVariable&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hi, I'm local to jsClosure()&lt;/span&gt;&lt;span class="dl"&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Global variables are accessible in jsClosure() and everywhere&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;globalVariable&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;I am local to jsClosure() and will not be accessible outside of my
   scope. Executing this will actually not work at all and lead to
   an error&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
   &lt;span class="nx"&gt;localVariable&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;To elaborate further, closures and scopes can be nested.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;globalVariable&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hi, I'm global and can be accessed anywhere&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;jsClosure&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;localVariable&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hi, I'm local to jsClosure()&lt;/span&gt;&lt;span class="dl"&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;I am global so I am accessible in jsClosure() and everywhere&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;globalVariable&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;jsInnerClosure&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;innerLocalVariable&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hi, I'm local to jsInnerClosure()&lt;/span&gt;&lt;span class="dl"&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Globally accessible variables can be accessed here and
       everywhere&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="nx"&gt;globalVariable&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Variables defined in the outer scope are also accessible
       in here. In this case localVariable&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
       &lt;span class="nx"&gt;localVariable&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;h2&gt;
  
  
  Asynchronous behavior
&lt;/h2&gt;

&lt;p&gt;A major benefit to JavaScript is the asynchronous functionality built into the language. This allows us to write code that is “non-blocking” which is crucial for user experience when some piece of code can take an indefinite amount of time to execute.&lt;/p&gt;

&lt;p&gt;Below is an example of how we asynchronously fetch transactions for my PAN 🍳 tokens that do not block the execution of the next &lt;code&gt;console.log&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;I am going to print 1st&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nx"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;https://api-ropsten.etherscan.io/api?module=account&amp;amp;action=tokentx&amp;amp;contractaddress=0x3723268a20af802e37958ea2b37e2ba8ffc9cf17&amp;amp;page=1&amp;amp;offset=100&amp;amp;sort=asc&lt;/span&gt;&lt;span class="dl"&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;then&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="nx"&gt;response&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;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;json&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;then&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="nx"&gt;panTransactions&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;I will not print 2nd because I am asynchronous and will print
       3rd after I am done processing&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="nx"&gt;panTransactions&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Although I am 3rd to execute, I will print 2nd&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;If you are going to program in JavaScript, understanding Closure and Scope and Asynchronous will be helpful for writing effective code.&lt;/p&gt;

&lt;h2&gt;
  
  
  Related Content
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Glossary/Scope"&gt;scope&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Closures"&gt;closure&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Asynchronous"&gt;asynchrony&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;If you found this content useful and like to get updates on new content, follow me on Twitter &lt;a href="https://twitter.com/itspanw"&gt;@itspanw&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>tutorial</category>
      <category>gettingstarted</category>
    </item>
  </channel>
</rss>
