<?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: Michael F</title>
    <description>The latest articles on DEV Community by Michael F (@mike26mbsc).</description>
    <link>https://dev.to/mike26mbsc</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%2F963633%2Fa7089dce-fde3-4a56-893a-487d05240ebe.png</url>
      <title>DEV Community: Michael F</title>
      <link>https://dev.to/mike26mbsc</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mike26mbsc"/>
    <language>en</language>
    <item>
      <title>Asynchronous JavaScript 2: Callbacks</title>
      <dc:creator>Michael F</dc:creator>
      <pubDate>Thu, 26 Jan 2023 04:10:01 +0000</pubDate>
      <link>https://dev.to/mike26mbsc/asynchronous-javascript-2-callbacks-3h95</link>
      <guid>https://dev.to/mike26mbsc/asynchronous-javascript-2-callbacks-3h95</guid>
      <description>&lt;p&gt;This is part two of an ongoing series on basic asynchronous operations for JavaScript. If you haven't checked out my first article go check it out it serves as a great introduction into the reason behind why understanding asynchronous techniques are so important to us as JavaScript Developers. &lt;/p&gt;

&lt;p&gt;The reason why we are covering callbacks today is because prior to the release of ES6 and ES7, callback functions were one of the only ways to handle asynchronous operations. ES6 and ES7 gave us promises and async/await syntax, which is what most API's utilize today. So it is incredibly important to understand how callback functions work and what they are used for in this context.&lt;/p&gt;

&lt;p&gt;A callback function is simply a function that gets passed into another function as a argument. And they are generally used to have better control over the execution sequence. Say if you wanted a function retrieve some data and another function to output the data.&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="cm"&gt;/** 
this function takes in whatever callback you want, 
prompts a user for input and runs the callback with
user input as an argument
*/&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;processUserInput&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;someText&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;prompt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Enter in some text: &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nf"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;someText&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;//call the function and pass in the callback&lt;/span&gt;
&lt;span class="nf"&gt;processUserInput&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;name&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="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Hello, &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&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;In this example we are defining a function that takes in any callback function that you want. It then prompts the user for input. After the input is collected from the user, it is then passed into the callback function to be alerted to the screen. &lt;/p&gt;

&lt;p&gt;This, at its most basic, is how callbacks are used. It's that simple. They are just a way of controlling execution sequence. &lt;/p&gt;

&lt;p&gt;In the below example you will see another basic example of how callback functions could be used.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;callbacks.js&lt;/strong&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="c1"&gt;//start by defining a function that outputs a string passed to it. &lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;displayMsg&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;msg&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="nx"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;//ok now write a driver function that takes in a callback.&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;mainFunction&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;

    &lt;span class="c1"&gt;//pass a string into the callback&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;myMsg&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;this is a string passed into the callback&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
    &lt;span class="nf"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;myMsg&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;   
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;//now you can pass in the function you defined at the beginning&lt;/span&gt;
&lt;span class="nf"&gt;mainFunction&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;displayMsg&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;//or you can just  define a function to run instead. &lt;/span&gt;
&lt;span class="nf"&gt;mainFunction&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;msg&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="nx"&gt;msg&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;Output:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;dev@localhost$node callbacks.js
this is a string passed into the callback
this is a string passed into the callback
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is a simple example. &lt;/p&gt;

&lt;p&gt;It is important to note that using callbacks in this way is best for simple examples only. Callbacks have a way of making code difficult to read and even worse to debug when you find yourself buried 3 functions deep in the execution stack trying to figure out which callback on which line of code isn't working right.&lt;/p&gt;

&lt;p&gt;For instance. Say we were building a house. First we get materials, then we build the framing, then finish the interior, then move in the house.&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;buildHouse&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;callback&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="nf"&gt;getMaterials&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;function &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;materials&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;buildFraming&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;materials&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;function &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;finishedFraming&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nf"&gt;finishInterior&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;function &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;finishedInterior&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;moveIn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;finishedFraming&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;finishedInterior&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;function &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;house&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="nf"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;house&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;p&gt;It doesn't take long for a program to become difficult to read. &lt;br&gt;
Thankfully there is a solution. In JavaScript we can implement asynchronous behavior through the use of Promises and Async/Await. Coming up in the next article. &lt;/p&gt;

</description>
      <category>codenewbie</category>
      <category>fullstack</category>
      <category>svelte</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Asynchronous JavaScript</title>
      <dc:creator>Michael F</dc:creator>
      <pubDate>Thu, 26 Jan 2023 04:01:47 +0000</pubDate>
      <link>https://dev.to/mike26mbsc/asynchronous-javascript-10fc</link>
      <guid>https://dev.to/mike26mbsc/asynchronous-javascript-10fc</guid>
      <description>&lt;p&gt;Have you ever needed to run one function based on the results of a different function? Needed to validate input before submitting it to a database server? Or maybe even wanted to display data to the screen without triggering a page reload. &lt;/p&gt;

&lt;p&gt;These are all examples of situations where you would want to employ asynchronous techniques in your code. But what do I mean by that?&lt;/p&gt;

&lt;p&gt;Well, computers are designed to be asynchronous, meaning that things can be done outside of the main thread. It is in the nature of computers to halt execution of one program to allow another to execute in the meantime. &lt;/p&gt;

&lt;p&gt;While most high-level programming languages like C and Java are synchronous by nature, most of them have ways of handling asynchronous operations. JavaScript is no different, except in the way that while some languages have the capability of spawning new threads. JavaScript lacks this capability.&lt;/p&gt;

&lt;p&gt;As a synchronous language, JavaScript is single-threaded. The JavaScript Engine has one execution call stack and one memory heap and therefore, functions synchronously. Parsing code line by line in sequential order storing variables and functions in memory.&lt;/p&gt;

&lt;p&gt;When the engine begins looking at your code, it creates the global execution context. &lt;/p&gt;

&lt;p&gt;Whenever a function gets called, a new local execution context gets created and when the engine comes across a function call, it enters the local execution context and continues parsing in there.&lt;/p&gt;

&lt;p&gt;Functions are pushed to the call stack and executed in a last-in-first-out manner, always executing what is at the top of the call stack first.&lt;/p&gt;

&lt;p&gt;This works fine for much of what we want to do in JavaScript. But what if we encounter code that holds up this process? Our entire application locks up because a function is taking a long time to run. What we need to be able to do is to start a function and run it in the background while the rest of the program is free to respond to other events. This is the problem that asynchronous techniques are designed to solve.&lt;/p&gt;

&lt;p&gt;In order to effectively leverage asynchronous techniques in your JavaScript projects. It is best to have a good understanding of topics such as callback functions, promises, and async/await syntax. This is one of the primary reasons why asynchronous techniques are a considered more advanced subject for a new developer. While it is a relatively simple topic to explain, it can be uniquely challenging to master and put into practice. &lt;/p&gt;

&lt;p&gt;At its core, it is simply a technique that allows you to control exactly how a program will behave if it encounters any blocking code on the execution call stack. It allows for these time-consuming parts to be run in the background without interrupting the other functions of the application.&lt;/p&gt;

&lt;p&gt;Asynchronous programming shines in its ability to handle situations such as this. Over the years developers have come up with techniques for handling these situations. By the time you are finished with this series you will have a solid understanding of how to employ this in your projects.  &lt;/p&gt;

&lt;p&gt;Follow me in this series and we will cover Callbacks, Promises and Async/Await to level up your understanding. Starting with callbacks. &lt;/p&gt;

</description>
      <category>javascript</category>
      <category>php</category>
      <category>programming</category>
      <category>discuss</category>
    </item>
  </channel>
</rss>
