<?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>Forem: jbroomer</title>
    <description>The latest articles on Forem by jbroomer (@jbroomer).</description>
    <link>https://forem.com/jbroomer</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%2F412720%2F952baa77-6ac0-4e47-bfb8-df27307e7b90.jpeg</url>
      <title>Forem: jbroomer</title>
      <link>https://forem.com/jbroomer</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/jbroomer"/>
    <language>en</language>
    <item>
      <title>Prefetching  Images in JS</title>
      <dc:creator>jbroomer</dc:creator>
      <pubDate>Tue, 19 Jan 2021 13:05:02 +0000</pubDate>
      <link>https://forem.com/jbroomer/prefetching-images-in-js-53nc</link>
      <guid>https://forem.com/jbroomer/prefetching-images-in-js-53nc</guid>
      <description>&lt;p&gt;&lt;em&gt;Loading...&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;These are mad times we live in, mad! There is an insatiable demand for a silky smooth user experience, instantaneous loading times, and unlimited content.&lt;/p&gt;

&lt;p&gt;When thinking about load times, one thing that comes to mind is images. Have you ever experienced the frustration of being on a loaded webpage, only to have to wait for images to load on further interaction with the page. &lt;em&gt;Of course you have.&lt;/em&gt; If you are developing a website with many images you will undoubtedly want to spare your users from this vexation.&lt;/p&gt;

&lt;p&gt;Luckily, there is a simple way to go about this if:  &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;You have the image URLs ahead of time
&lt;/li&gt;
&lt;li&gt;You can accurately predict which images the user will likely see next
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If the above isn't true then this article may not be super useful to you.&lt;/p&gt;

&lt;h3&gt;
  
  
  Basic Prefetching
&lt;/h3&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;urls&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
  &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;shorturl.at/auLWY&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;shorturl.at/sBTX5&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="nx"&gt;urls&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="nx"&gt;imageUrl&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;const&lt;/span&gt; &lt;span class="nx"&gt;imageElement&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Image&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// Create new image element&lt;/span&gt;
    &lt;span class="nx"&gt;imageElement&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;onload&lt;/span&gt; &lt;span class="o"&gt;=&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="c1"&gt;// Do something on success&lt;/span&gt;
    &lt;span class="nx"&gt;imageElement&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;onerror&lt;/span&gt; &lt;span class="o"&gt;=&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="c1"&gt;// Do something on error&lt;/span&gt;
    &lt;span class="nx"&gt;imageElement&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;src&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;imageUrl&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Set the image URL which will trigger a network request&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's talk about why the approach above actually works.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;Image&lt;/code&gt; constructor, which is functionally equivalent to &lt;code&gt;document.createElement('img')&lt;/code&gt;, creates a new image instance. The purpose of creating these images is not for display, but rather to set the image &lt;code&gt;src&lt;/code&gt; attribute which in turn will trigger a network request for the image. If you can accurately predict which images the user is likely to see next based on some logical flow of interaction, then you can ensure the images have already been delivered to the browser by the time the user gets there. &lt;/p&gt;

&lt;p&gt;This may seem trivial, but for large scale applications it can save a significant amount of time. In order for this to work effectively, you will need to find a practical place to prefetch in your current environment: &lt;strong&gt;before&lt;/strong&gt; the user sees the images and &lt;strong&gt;after&lt;/strong&gt; any heavy initialization tasks as to not affect your web application's initial load time.&lt;/p&gt;

&lt;h3&gt;
  
  
  Prefetching w/Promises
&lt;/h3&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;urls&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
  &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;shorturl.at/auLWY&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;shorturl.at/sBTX5&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;all&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;urls&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;imageUrl&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&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;const&lt;/span&gt; &lt;span class="nx"&gt;imageElement&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Image&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// Create new image element&lt;/span&gt;
    &lt;span class="nx"&gt;imageElement&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;onload&lt;/span&gt; &lt;span class="o"&gt;=&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;resolve&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt; &lt;span class="c1"&gt;// Do something on success&lt;/span&gt;
    &lt;span class="nx"&gt;imageElement&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;onerror&lt;/span&gt; &lt;span class="o"&gt;=&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;reject&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt; &lt;span class="c1"&gt;// Do something on error&lt;/span&gt;
    &lt;span class="nx"&gt;imageElement&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;src&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;imageUrl&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Set the image URL which will trigger a network request&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="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{})&lt;/span&gt; &lt;span class="c1"&gt;// Do something after all images load&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="nx"&gt;err&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="c1"&gt;// Do something if there are any errors&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Additionally, if find it necessary to have more control over the loading state of your images, such as triggering some function when they &lt;strong&gt;all&lt;/strong&gt; load successfully or to conditionally display some skeleton component, there is an easy way to do so using promises.&lt;/p&gt;

&lt;p&gt;Simply map each image url to a new promise and resolve/reject the promise as it suits your needs. Wrap the promise array in a &lt;code&gt;Promise.all&lt;/code&gt; and use the &lt;code&gt;then&lt;/code&gt; and &lt;code&gt;catch&lt;/code&gt; methods to execute the appropriate action. That's it!&lt;/p&gt;

&lt;p&gt;I hope this article has helped you understand how you can use prefetching to improve your web application's user experience. Let me know in the comments if it works for you!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>tutorial</category>
      <category>programming</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Node.js + Cron === 💪🏼💪🏼💪🏼</title>
      <dc:creator>jbroomer</dc:creator>
      <pubDate>Fri, 10 Jul 2020 12:30:21 +0000</pubDate>
      <link>https://forem.com/jbroomer/node-js-cron-5gg0</link>
      <guid>https://forem.com/jbroomer/node-js-cron-5gg0</guid>
      <description>&lt;p&gt;&lt;strong&gt;TLDR; wrote script to automate new gym registrations everyday. Tell me in the comments below what you have done to solve your COVID quarantine problems.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;CODING FOR COVID&lt;/strong&gt; Everyone has been affected by COVID-19 in some way. Many states were shutdown for an extended period of time, something that I don't think most people have come close to experiencing. But that doesn't mean life can't go on! As programmers we are to be critical thinkers, looking to solve our problems and hinderances. I want to talk about one that I solved and hear about your own! I call it: &lt;strong&gt;Coding for Covid&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Businesses in my area have begun their reopening. I guess I should really write, &lt;em&gt;reopening*****&lt;/em&gt;, since there are numerous strict health and safety guidelines. One such business reopening I have been anticipating the most is the gym, which naturally have some of the strictest requirements. For example, almost 2/3 of the gym equipment is closed off, the usual 24/7 gym hours have been cut back to 7:00am-6:30pm (with an hour in-between that they close for cleaning), and to top it all off, you have to register two days in advance for one of the allotted time slots that has a maximum capacity of 25 people. &lt;em&gt;This is a large facility&lt;/em&gt;. I'm sure you can imagine how quickly the most popular time slots fill up.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;First, let me be clear, I am completely on board with the guidelines and keeping everyone safe.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;As dedicated to my workout regimen as I am, time slots only open two days in advance and I simply cannot be online at the exact time each day to secure a slot. &lt;em&gt;So, this got me thinking&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;I took one look at the network requests on the registration website and knew I could start cooking something up. I decided to use &lt;strong&gt;Node.js&lt;/strong&gt; to write the script and &lt;strong&gt;axios&lt;/strong&gt; to make the network request with the proper authentication. A couple hours later I successfully signed up to a class through my terminal.&lt;/p&gt;

&lt;p&gt;Now, I needed this to run at the same time every day so obviously my head immediately went to Cron Jobs. However, I don't have a machine that will, reliably, be on all the time. &lt;/p&gt;

&lt;p&gt;&lt;em&gt;Cue in: Google Compute Engine&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;I configured a super low-end linux machine: lowest amount of RAM, standard 10GB storage, etc. I pulled down my script file from git and 20 minutes later I was ready to go.&lt;/p&gt;

&lt;p&gt;Of course I took proper precaution to not sign up for any &lt;em&gt;at risk&lt;/em&gt; time slots as, thankfully, I am not at risk. My next plan is to configure some quick commands to cancel my registrations to free up time for everyone else in the event that I cannot make it on a given day.&lt;/p&gt;

&lt;h3&gt;
  
  
  What have you done to Code for Covid?
&lt;/h3&gt;

</description>
      <category>javascript</category>
      <category>node</category>
      <category>discuss</category>
      <category>learning</category>
    </item>
    <item>
      <title>Information Error: Duplication Overload!?</title>
      <dc:creator>jbroomer</dc:creator>
      <pubDate>Tue, 30 Jun 2020 21:32:53 +0000</pubDate>
      <link>https://forem.com/jbroomer/information-error-duplication-overload-bkk</link>
      <guid>https://forem.com/jbroomer/information-error-duplication-overload-bkk</guid>
      <description>&lt;p&gt;&lt;strong&gt;Edit:&lt;/strong&gt; Discuss your most used resources/authors in the comments below!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;TLDR; Thank you to anyone who has ever answered online questions or made online tutorials.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Information.&lt;/em&gt;&lt;/strong&gt; Perhaps one of the most notable characteristics of our time is the sheer amount of information available at our finger tips. Now, of course someone can (and will) play Devil's advocate and exacerbate all of the "disservices" being done with that amount of information.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Well, I'm only here to talk about the &lt;em&gt;good&lt;/em&gt;.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Thought Experiment Time:&lt;/strong&gt; close your eye's and.... &lt;br&gt;
 Just kidding don't do that. If you've already done it then it's too late, otherwise, keep reading.&lt;/p&gt;

&lt;p&gt;Imagine yourself back in your Intro to Computer Science, Intro to Engineering, or whatever Intro course may have been your first; a time where terms such as &lt;code&gt;Object Oriented Programming&lt;/code&gt;, &lt;code&gt;Higher Order Functions&lt;/code&gt;, &lt;code&gt;Tree Searches&lt;/code&gt; may have had little meaning to such an innocent mind. If you are still at this point, bless your soul. If not, perhaps these memories have been repressed due the severe, irreversible, damage it may have caused otherwise, but try to remember.&lt;/p&gt;

&lt;p&gt;Still in the shoes, of your younger, irreproachable, self. Do you remember the hundreds if not thousands of articles, blog posts, video tutorials, etc. All tailored to your specific topic and each one slightly different than the last. We all learn in different ways so, if one author didn't get through to you then perhaps the next one made it &lt;strong&gt;click&lt;/strong&gt;! &lt;/p&gt;

&lt;p&gt;That is really what this post is about, it is a &lt;em&gt;thank you&lt;/em&gt;. To all of the developers, educators, and anyone else out there that has used their time to answer a question or post a tutorial.&lt;/p&gt;

&lt;h2&gt;
  
  
  We appreciate you!
&lt;/h2&gt;

</description>
      <category>beginners</category>
      <category>leadership</category>
      <category>productivity</category>
    </item>
  </channel>
</rss>
