<?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: Andrew Tamburino</title>
    <description>The latest articles on DEV Community by Andrew Tamburino (@atamburino).</description>
    <link>https://dev.to/atamburino</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%2F2114180%2Fa23ba2b8-9ec1-4760-9f7e-549698baa3c1.png</url>
      <title>DEV Community: Andrew Tamburino</title>
      <link>https://dev.to/atamburino</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/atamburino"/>
    <language>en</language>
    <item>
      <title>JavaScript the .then / .when method.</title>
      <dc:creator>Andrew Tamburino</dc:creator>
      <pubDate>Wed, 16 Oct 2024 15:27:56 +0000</pubDate>
      <link>https://dev.to/atamburino/javascript-the-then-when-method-42hi</link>
      <guid>https://dev.to/atamburino/javascript-the-then-when-method-42hi</guid>
      <description>&lt;p&gt;Today, I was following a WordPress tutorial, and the instructor was demonstrating how to search the site using jQuery and JavaScript, specifically using the .then() and .when() methods. As I followed along, I realized that I wasn’t as familiar with these methods as I thought.&lt;/p&gt;

&lt;p&gt;So, I did a bit of research, took some notes, and added them to my Obsidian notebook. To be honest, the notes are kind of rough—they look like something a fifth grader with the attention span of a squirrel might put together (which, let's face it, sometimes feels like me!).&lt;/p&gt;

&lt;p&gt;But I figured, why not share them? If they help even one person, it’s worth posting.&lt;/p&gt;




&lt;h3&gt;
  
  
  Imagine this:
&lt;/h3&gt;

&lt;p&gt;You're at school, and you and your friends make &lt;strong&gt;promises&lt;/strong&gt; to each other. These promises are like saying, “I’ll finish my homework, and then we can play games.”&lt;/p&gt;

&lt;p&gt;But here's the thing: sometimes it takes a little while to finish the homework. You might finish it fast, or maybe your dog eats it, and you never finish (oops!). &lt;strong&gt;JavaScript promises&lt;/strong&gt; work the same way. It waits for something to happen, and then it says, “Okay, what next?”&lt;/p&gt;

&lt;h3&gt;
  
  
  Now, let’s talk about &lt;code&gt;.then()&lt;/code&gt;:
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;.then()&lt;/code&gt; method is like what you’ll do &lt;strong&gt;after&lt;/strong&gt; the promise is done. Imagine you say to your friend:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;“If I finish my homework, &lt;strong&gt;then&lt;/strong&gt; we can play video games!”&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;But what if you don’t finish? Uh-oh, you can add a &lt;strong&gt;backup plan&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;“If I don’t finish my homework, &lt;strong&gt;then&lt;/strong&gt; we won’t play video games.”&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here’s how you would say this in JavaScript:&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;promiseToFinishHomework&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="o"&gt;=&amp;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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Yay! We can play games!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// If you finished&lt;/span&gt;
  &lt;span class="p"&gt;})&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;catch&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="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;Oh no! No games today!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// If you didn’t finish&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Now for &lt;code&gt;.when()&lt;/code&gt;:
&lt;/h3&gt;

&lt;p&gt;This is like when you’re waiting for &lt;strong&gt;a bunch of your friends&lt;/strong&gt; to finish their homework so you can all play together. You’re like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;“When &lt;strong&gt;all of us finish&lt;/strong&gt;, then we can play together!”&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In real life, that’s called &lt;strong&gt;working together&lt;/strong&gt;. In JavaScript, you use something called &lt;code&gt;Promise.all()&lt;/code&gt; (which is just like saying "Let's wait for everyone to be ready!"):&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="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;all&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="nx"&gt;friend1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;friend2&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="o"&gt;=&amp;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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;We’re all done! Time to play!&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;
  
  
  Summary (Super Simple):
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;.then()&lt;/code&gt;&lt;/strong&gt; Is like saying “When this one thing finishes, then we’ll do something fun.”&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;.when()&lt;/code&gt;&lt;/strong&gt; (or &lt;code&gt;Promise.all()&lt;/code&gt;) is like saying “When &lt;strong&gt;everyone&lt;/strong&gt; finishes their work, then we’ll all play together.”&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So, JavaScript is just making sure it knows &lt;strong&gt;what happens next&lt;/strong&gt; when promises are kept—or broken! 😄 &lt;/p&gt;




&lt;h3&gt;
  
  
  So How Does It Know?
&lt;/h3&gt;

&lt;p&gt;The promise doesn’t check the homework by itself. &lt;strong&gt;You tell it&lt;/strong&gt;! When you create a promise, you decide if it’s &lt;strong&gt;resolved&lt;/strong&gt; or &lt;strong&gt;rejected&lt;/strong&gt; based on whether the task (like finishing homework) is done or not. The &lt;code&gt;.then()&lt;/code&gt; runs when the promise is resolved, and &lt;code&gt;.catch()&lt;/code&gt; runs when it’s rejected.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example With Real Life Action:
&lt;/h3&gt;

&lt;p&gt;Here’s a full example to see it in action:&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;let&lt;/span&gt; &lt;span class="nx"&gt;promiseToFinishHomework&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;reject&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="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;finishedHomework&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Change this to false to see the rejection&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;finishedHomework&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// Homework is done!&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// Homework is not done!&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="nx"&gt;promiseToFinishHomework&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="o"&gt;=&amp;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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Yay! We can play games!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// If the promise resolves (homework done)&lt;/span&gt;
  &lt;span class="p"&gt;})&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;catch&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="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;Oh no! No games today!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// If the promise rejects (homework not done)&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;

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

&lt;/div&gt;



</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Why Test-Driven Development (TDD) Is a Game Changer for Better Code</title>
      <dc:creator>Andrew Tamburino</dc:creator>
      <pubDate>Mon, 23 Sep 2024 14:12:18 +0000</pubDate>
      <link>https://dev.to/atamburino/why-test-driven-development-tdd-is-a-game-changer-for-better-code-3kl3</link>
      <guid>https://dev.to/atamburino/why-test-driven-development-tdd-is-a-game-changer-for-better-code-3kl3</guid>
      <description>&lt;p&gt;🧪 Exploring Test-Driven Development (TDD) has been an eye-opener! Writing tests before the code forces you to think through functionality and edge cases upfront. It’s a powerful way to build confidence in your code and ensure each feature works as expected before you even start coding.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why TDD?
&lt;/h3&gt;

&lt;p&gt;The concept of writing tests before code might feel counterintuitive at first, but it offers some key benefits:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Prevents overengineering&lt;/strong&gt;: You only write the code needed to pass the test.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Early bug detection&lt;/strong&gt;: Catch issues as early as possible, reducing troubleshooting time later.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Well-defined requirements&lt;/strong&gt;: The tests act as a clear, actionable specification for what the code should do.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  My Experience
&lt;/h3&gt;

&lt;p&gt;Recently, my mentor had us walk through some files with pre-written tests. Our goal was to draft functions that met the tests and think through the process and the best approach. &lt;/p&gt;

&lt;p&gt;In this exercise, I noticed a pattern: each test had a step to validate the string input. Instead of rewriting the validation logic in every function, I created a simple helper function to handle the validation for all the tests. This not only made my code cleaner, but it also improved efficiency. &lt;/p&gt;

&lt;p&gt;TDD opened my eyes not just to writing better code but to &lt;strong&gt;thinking more strategically&lt;/strong&gt; about how I code. It really drives how you can be more efficient in your approach.&lt;/p&gt;

&lt;h3&gt;
  
  
  How It Works
&lt;/h3&gt;

&lt;p&gt;TDD follows a simple three-step process often referred to as &lt;strong&gt;Red-Green-Refactor&lt;/strong&gt;:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Red&lt;/strong&gt;: Write a test for a new feature or function. At this point, the test will fail because the feature doesn’t exist yet.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Green&lt;/strong&gt;: Write the minimum amount of code to make the test pass.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Refactor&lt;/strong&gt;: Clean up the code while ensuring that the tests still pass.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Have You Tried TDD?
&lt;/h3&gt;

&lt;p&gt;Have you experimented with TDD in your projects? What were your challenges and wins? Let’s chat in the comments — I’d love to hear your experience with this methodology!&lt;/p&gt;

</description>
      <category>tdd</category>
      <category>javascript</category>
      <category>beginners</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
