<?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: Nena</title>
    <description>The latest articles on DEV Community by Nena (@nena).</description>
    <link>https://dev.to/nena</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%2F216048%2Fb249aab3-41d6-4c88-bde4-b9fde76c86ba.png</url>
      <title>DEV Community: Nena</title>
      <link>https://dev.to/nena</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/nena"/>
    <language>en</language>
    <item>
      <title>Understanding callbacks - They aren’t as hard as you think!</title>
      <dc:creator>Nena</dc:creator>
      <pubDate>Mon, 09 Nov 2020 16:07:23 +0000</pubDate>
      <link>https://dev.to/nena/understanding-callbacks-2m57</link>
      <guid>https://dev.to/nena/understanding-callbacks-2m57</guid>
      <description>&lt;h1&gt;
  
  
  Why callbacks aren't as hard as you think
&lt;/h1&gt;

&lt;p&gt;When I started my first job as a JavaScript developer I was heavily confused by callbacks. And by heavily confused I mean thoroughly and utterly confused. With my little knowledge of JavaScript at that time I had only stumbled across callbacks while using event listeners or timeouts, but didn't really notice them and didn't even try to think about how these functions actually work. &lt;/p&gt;

&lt;p&gt;So when I had to use callbacks actively for the first time, I was just lost. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/CPskAi4C6WLHa/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/CPskAi4C6WLHa/giphy.gif" width="500" height="228"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;But how did I solve this problem? To be honest: I didn't. I pushed it aside for a few weeks and hoped it wouldn't bother me again. But suddenly a wave of determination hit me and one evening I started my research into the world of callbacks again and guess what? In a few minutes I somehow grasped the concept of a problem that had been bugging me for weeks by then. I don't know if it was because I suddenly found the perfect explanation or because I wasn't stressing myself out anymore but I remember feeling so proud and suddenly wanted to use callbacks for every possible problem. &lt;/p&gt;

&lt;p&gt;I'm telling you this little story because I want to encourage you to never give up developing. Everybody is going to face problems while learning to program one day, don't let them get you down. I'm sure you'll be able to tackle them! :) So let's get into it. &lt;/p&gt;

&lt;h2&gt;
  
  
  What exactly is a callback?
&lt;/h2&gt;

&lt;p&gt;To put it into simple words: A callback is a function that is passed into another function as an argument – just like you would do with variables. &lt;/p&gt;

&lt;p&gt;The function is then going to be called inside the outer function.&lt;/p&gt;

&lt;h2&gt;
  
  
  How do I use them?
&lt;/h2&gt;

&lt;p&gt;Let's have a look at a simple example:&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;function&lt;/span&gt; &lt;span class="nx"&gt;myName&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="nx"&gt;callback&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="s1"&gt;Hi, my name is &lt;/span&gt;&lt;span class="dl"&gt;'&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="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;function&lt;/span&gt; &lt;span class="nx"&gt;myCallback&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="s1"&gt;and this is how callbacks work!&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;myName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Nena&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;myCallback&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// output:&lt;/span&gt;
&lt;span class="c1"&gt;// Hi, my name is Nena&lt;/span&gt;
&lt;span class="c1"&gt;// and this is how callbacks work!&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Anonymous callbacks
&lt;/h3&gt;

&lt;p&gt;Alternatively, if you don't plan on using your callback function again, you can define it directly in the function call by using an anonymous function:&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;function&lt;/span&gt; &lt;span class="nx"&gt;myName&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="nx"&gt;callback&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="s1"&gt;Hi, my name is &lt;/span&gt;&lt;span class="dl"&gt;'&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="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="nx"&gt;myName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Nena&lt;/span&gt;&lt;span class="dl"&gt;'&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;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="s1"&gt;and this is how callbacks work!&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="c1"&gt;// output:&lt;/span&gt;
&lt;span class="c1"&gt;// Hi, my name is Nena&lt;/span&gt;
&lt;span class="c1"&gt;// and this is how callbacks work!&lt;/span&gt;

&lt;span class="c1"&gt;// Notice how the output is the same as before.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see, the declared function has no name and is therefore called an &lt;strong&gt;anonymous function&lt;/strong&gt;. &lt;/p&gt;

&lt;h2&gt;
  
  
  And why do I need callbacks?
&lt;/h2&gt;

&lt;p&gt;If you have made it this far you've probably (or hopefully) understood the concept of callbacks by now – Congratulations! But you may be wondering why you would ever need this. So stay tuned, here's why: &lt;/p&gt;

&lt;p&gt;JavaScript usually runs your code from top to bottom. But since it's an event-driven language you sometimes need to wait for something else to finish before you can continue with your script. This is called &lt;strong&gt;asynchronous programming&lt;/strong&gt;. That's where callbacks come in handy. &lt;/p&gt;

&lt;p&gt;By using callbacks you can ensure that your script waits for a task to finish before it continues. &lt;/p&gt;

&lt;p&gt;This is especially important when you're requesting information from an http or API request. You can define a callback which only executes after your response is ready and then handles the information. &lt;/p&gt;

&lt;p&gt;Here's a simple fictitious example:&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="nx"&gt;myAPI&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;request&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="c1"&gt;// process your response here&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Callbacks and event listeners
&lt;/h3&gt;

&lt;p&gt;But even if you didn't work with API requests yet, you probably still used callbacks before. Maybe even without even noticing them, just like I didn't. Callbacks are the key concept of event listeners. Whenever you're using the &lt;strong&gt;addEventListener&lt;/strong&gt; function, you're using callbacks. The function waits for the event to happen and then invokes a callback function.&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="nx"&gt;myElement&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;click&lt;/span&gt;&lt;span class="dl"&gt;'&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="c1"&gt;// this is a callback&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  You did it!
&lt;/h2&gt;

&lt;p&gt;By now I hope that I successfully was able to teach you everything you need to know to start working with callbacks. If you have any questions feel free to ask them in the comment section below! :) &lt;/p&gt;

&lt;p&gt;If you don't, congratulations! I'm proud of you for understanding this concept which bugs a lot of new JavaScript developers. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/xT0xezQGU5xCDJuCPe/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/xT0xezQGU5xCDJuCPe/giphy.gif" width="498" height="225"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Easily parse an excel spreadsheet into JSON</title>
      <dc:creator>Nena</dc:creator>
      <pubDate>Wed, 28 Oct 2020 13:21:20 +0000</pubDate>
      <link>https://dev.to/nena/easily-parse-an-excel-spreadsheet-into-json-2dg7</link>
      <guid>https://dev.to/nena/easily-parse-an-excel-spreadsheet-into-json-2dg7</guid>
      <description>&lt;p&gt;Did you know that you can easily parse an excel spreadsheet using Node.js? Keep reading to speed up your information retrieval processes.&lt;/p&gt;

&lt;h1&gt;
  
  
  Why do I need this?
&lt;/h1&gt;

&lt;p&gt;I learned this a few months ago and came back to using it ever since. I've found that there are several use cases in my daily life that can profit from this little script - in my job as well as in my private life or side projects. You can use it for nearly every scenario where you need to transform your spreadsheet into JSON. &lt;/p&gt;

&lt;h2&gt;
  
  
  Here are some examples:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;your working with a client and need an easy way for them to maintain the data your using for the project, most people are familiar with working with excel&lt;/li&gt;
&lt;li&gt;your collaborating with a few people and want a safe place for everybody to collect data&lt;/li&gt;
&lt;li&gt;you like planning your life in excel but want to process the data further - Maybe you want to create a beautiful website about your carefully planned road trip &lt;/li&gt;
&lt;li&gt;and many more, get creative! &lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  How do I use it?
&lt;/h1&gt;

&lt;p&gt;You only need two Node.js modules for this: &lt;a href="https://github.com/DaSpawn/xlsx-stream-reader/issues"&gt;&lt;strong&gt;xlsx-stream-reader&lt;/strong&gt;&lt;/a&gt; for parsing the data and &lt;a href="https://nodejs.org/api/fs.html"&gt;&lt;strong&gt;fs&lt;/strong&gt;&lt;/a&gt; for saving the created JSON file.  &lt;/p&gt;

&lt;p&gt;xlsx-stream-reader parses each row of your spreadsheet and lets you process the data however you want. I usually save my data in an array, where each row represents one element: &lt;/p&gt;

&lt;h2&gt;
  
  
  Code example of the parsing process
&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;currentRow&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{};&lt;/span&gt;

&lt;span class="c1"&gt;// iterating the rows &lt;/span&gt;
&lt;span class="nx"&gt;row&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;values&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;forEach&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;rowVal&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;colNum&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;rowVal&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;rowVal&lt;/span&gt; &lt;span class="o"&gt;!==&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="c1"&gt;// parsing all columns &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;colNum&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;1&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;currentRow&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;rowVal&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// ID&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&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;colNum&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;2&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;currentRow&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;rowVal&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// name&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&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;colNum&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;3&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;currentRow&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;img&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;rowVal&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// img&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;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;currentRow&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="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;currentRow&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="c1"&gt;// push the current row into your array&lt;/span&gt;
    &lt;span class="nx"&gt;dataInRows&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;currentRow&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;The stream reader also emits events, e.g. when it reaches the end of the spreadsheet or an error occurs. &lt;/p&gt;

&lt;p&gt;Overall the script is pretty lightweight and short. The longest part is usually the one where you define the rows (see above). &lt;/p&gt;

&lt;h2&gt;
  
  
  Your input
&lt;/h2&gt;

&lt;p&gt;I hope you could learn something new from this little post. Feel free to add your thoughts or questions in the comments below! :) &lt;/p&gt;

</description>
      <category>node</category>
      <category>javascript</category>
      <category>productivity</category>
    </item>
  </channel>
</rss>
