<?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: Patrick Canella</title>
    <description>The latest articles on Forem by Patrick Canella (@pcanella).</description>
    <link>https://forem.com/pcanella</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%2F469873%2Fd5500c4a-58c5-446e-aa7f-8fb237524e16.JPG</url>
      <title>Forem: Patrick Canella</title>
      <link>https://forem.com/pcanella</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/pcanella"/>
    <language>en</language>
    <item>
      <title>How to add a Giphy image to Dev.to posts in 2023</title>
      <dc:creator>Patrick Canella</dc:creator>
      <pubDate>Fri, 17 Feb 2023 21:52:15 +0000</pubDate>
      <link>https://forem.com/pcanella/how-to-add-a-giphy-image-to-devto-posts-in-2023-50m0</link>
      <guid>https://forem.com/pcanella/how-to-add-a-giphy-image-to-devto-posts-in-2023-50m0</guid>
      <description>&lt;p&gt;I had issues with this so I thought I'd write a quick how-to on this. To the Dev.to folks: Why can't we easily embed this? There's no Giphy option in the embed tab. I tried using Giphy's embed as well, but it just would never show up.&lt;/p&gt;

&lt;p&gt;This is kind of a &lt;a href="https://www.timo-ernst.net/blog/2020/10/09/how-to-add-giphy-gifs-into-dev-to-posts/" rel="noopener noreferrer"&gt;rip off of this post&lt;/a&gt;, but Giphy has updated their site, so this is a tad different now.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Okay, so first choose a gif, we'll choose this Giphy link:
&lt;code&gt;https://giphy.com/gifs/natgeowild-america-natgeo-americathebeautiful-ItIMgSW1kSAo0QTl30&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Click &lt;strong&gt;Share&lt;/strong&gt; (see below)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8tbmcanjn5ey0imxe1rg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8tbmcanjn5ey0imxe1rg.png" alt="Clicking the share button on Giphy" width="800" height="415"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Click &lt;strong&gt;Copy Giphy Share Link&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ff481rgymszxvuwhu6c2g.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ff481rgymszxvuwhu6c2g.png" alt="How to copy gif link" width="800" height="353"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Take copied link from above and add it inside this tag:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;img&lt;/span&gt; &lt;span class="na"&gt;width=&lt;/span&gt;&lt;span class="s"&gt;"100%"&lt;/span&gt; &lt;span class="na"&gt;style=&lt;/span&gt;&lt;span class="s"&gt;"width:100%"&lt;/span&gt; &lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"https://media.giphy.com/media/v1.Y2lkPTc5MGI3NjExZWQ3NmZmMjMwODQwNjZiZTcwY2VhODhlMzE4NmRiZDYzYTNjNDM5MSZjdD1n/ItIMgSW1kSAo0QTl30/giphy.gif"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  End Result
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/v1.Y2lkPTc5MGI3NjExZWQ3NmZmMjMwODQwNjZiZTcwY2VhODhlMzE4NmRiZDYzYTNjNDM5MSZjdD1n/ItIMgSW1kSAo0QTl30/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/v1.Y2lkPTc5MGI3NjExZWQ3NmZmMjMwODQwNjZiZTcwY2VhODhlMzE4NmRiZDYzYTNjNDM5MSZjdD1n/ItIMgSW1kSAo0QTl30/giphy.gif" width="480" height="274"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>welcome</category>
    </item>
    <item>
      <title>Godot 4: "Pointing" an Area2D to the correct direction using look_at!.</title>
      <dc:creator>Patrick Canella</dc:creator>
      <pubDate>Fri, 17 Feb 2023 20:38:49 +0000</pubDate>
      <link>https://forem.com/pcanella/godot-4-pointing-an-area2d-to-the-correct-direction-using-lookat-58k2</link>
      <guid>https://forem.com/pcanella/godot-4-pointing-an-area2d-to-the-correct-direction-using-lookat-58k2</guid>
      <description>&lt;p&gt;Hello all! I just want to do a quick post regarding Godot 4, something I've been working with for awhile and absolutely &lt;strong&gt;LOVE.&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;If you don't know what Godot (pronounced &lt;em&gt;guh-doh&lt;/em&gt;)is, it's an open source game engine that's almost ready for its 4.0 release.&lt;/p&gt;

&lt;p&gt;Anyway, let's move on. One issue I had in my upcoming video game, Retro Hockey, is that I wanted a player to pass the puck to another player. I wanted them to have a "line of sight" (see below) for their passing in the shape of a big inverted triangle.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fev289n3ninqjsjnx2r8z.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fev289n3ninqjsjnx2r8z.png" alt="A Player with a giant triangle coming out of them, that is an Area2D" width="522" height="457"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Then, if there's another player on the same team inside this triangle, and they are closer than another player, it would pass to the closer player.&lt;/p&gt;

&lt;p&gt;Anyway, the problem I was having was I needed to figure out some way to rotate the triangle to the direction (&lt;code&gt;Vector2&lt;/code&gt;) the player was moving to.&lt;/p&gt;

&lt;p&gt;The solution? the &lt;code&gt;look_at&lt;/code&gt; function! &lt;/p&gt;

&lt;p&gt;In Godot 4, the &lt;code&gt;look_at&lt;/code&gt; function does this (&lt;a href="https://docs.godotengine.org/en/latest/classes/class_node2d.html#class-node2d-method-look-at" rel="noopener noreferrer"&gt;official docs&lt;/a&gt;): &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;code&gt;void look_at ( Vector2 point )&lt;/code&gt;&lt;br&gt;
Rotates the node so it points towards the point, which is expected to use global coordinates.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Great! So now, let's just take the existing &lt;code&gt;linear_velocity&lt;/code&gt; of my character/player. Based on how your character moves, it may be different (something to keep in mind is &lt;a href=""&gt;how Godot and game programming uses the Cartesian plane&lt;/a&gt;), but here's the example I have:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight gdscript"&gt;&lt;code&gt;&lt;span class="c1"&gt;# based on my char's movements, we reverse the x and y coords&lt;/span&gt;
&lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="n"&gt;newVec&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;Vector2&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;linear_velocity&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;linear_velocity&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# set the look_at point to the initial global position of our triangle origin + the new vector. We also have to account for the y coord being negative since Godot/game programming uses the plane a bit differently...&lt;/span&gt;
&lt;span class="n"&gt;passing_triangle&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;look_at&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;passing_triangle&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;global_position&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;Vector2&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;newVec&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;newVec&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;)))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So then, with the &lt;code&gt;View Collision Shapes&lt;/code&gt; option on, we can see how this works! FYI, if you're not sure how to do this, here's a handy image for you: &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fowg4dscx1p8umi7qbjxb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fowg4dscx1p8umi7qbjxb.png" alt="Godot Menu &amp;gt; Debug &amp;gt; Debug Collision Paths image" width="800" height="468"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And here's the final product! A giant triangle that rotates to where the player is going:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/v1.Y2lkPTc5MGI3NjExYmNlZWJjZDcxZWQyZDI3YTJmNjM1NzM0MzdlNzI5MTBhYWE2ZmFmZSZjdD1n/Ejmzjas5RMNauBiXhs/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/v1.Y2lkPTc5MGI3NjExYmNlZWJjZDcxZWQyZDI3YTJmNjM1NzM0MzdlNzI5MTBhYWE2ZmFmZSZjdD1n/Ejmzjas5RMNauBiXhs/giphy.gif" width="480" height="316"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This post was heavily inspired by this &lt;a href="https://www.reddit.com/r/godot/comments/j28xuj/how_to_make_an_area2d_rotate_based_on_the/" rel="noopener noreferrer"&gt;old Reddit thread on Godot&lt;/a&gt;! Thank you for the help, stranger!&lt;/p&gt;

</description>
      <category>godot</category>
      <category>gamedev</category>
      <category>beginners</category>
      <category>opensource</category>
    </item>
    <item>
      <title>🥘 Reduce: It's all gravy, baby!</title>
      <dc:creator>Patrick Canella</dc:creator>
      <pubDate>Mon, 22 Aug 2022 19:34:00 +0000</pubDate>
      <link>https://forem.com/pcanella/reduce-not-as-scary-as-it-may-look-55c9</link>
      <guid>https://forem.com/pcanella/reduce-not-as-scary-as-it-may-look-55c9</guid>
      <description>&lt;p&gt;I've been working in JS codebases for over 10 years (👴🏻) and one thing that &lt;em&gt;still&lt;/em&gt; trips me up sometimes is the &lt;code&gt;reduce&lt;/code&gt; function. Every time, I go to the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce" rel="noopener noreferrer"&gt;MDN documentation on &lt;code&gt;reduce&lt;/code&gt;&lt;/a&gt;, I have some initial trouble understanding how the heck it works.&lt;/p&gt;

&lt;p&gt;Then, if you go on Twitter or something, everyone will be like "reduce is the most important array function!" Cue anxiety 😩!&lt;/p&gt;

&lt;p&gt;So in this short post, I'll try to explain Reduce and show examples that are actually relevant and not overwhelming.&lt;/p&gt;

&lt;p&gt;So with that; let's REDUCE 🥘.&lt;/p&gt;

&lt;p&gt;Let's start with the "official" documentation explanation and go from there:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The reduce() method executes a user-supplied "reducer" callback function on each element of the array, in order, passing in the return value from the calculation on the preceding element. The final result of running the reducer across all elements of the array is a single value.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;From this we know:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It takes in a callback function &lt;code&gt;() =&amp;gt; {}&lt;/code&gt; for each array element&lt;/li&gt;
&lt;li&gt;It takes the previous calculated element and uses it in the callback function. Like so &lt;code&gt;(previousValue, currentValue) =&amp;gt; {}&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;It returns a &lt;strong&gt;new&lt;/strong&gt; array of values after reducing&lt;/li&gt;
&lt;li&gt;We don't know this really, but &lt;code&gt;reduce&lt;/code&gt; is a really stupid name for this function. IMO &lt;code&gt;accumulator&lt;/code&gt; would be a far better, and more accurate term.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;One thing to note is that &lt;code&gt;reduce&lt;/code&gt; will take a callback, but it will also take an &lt;code&gt;initial value&lt;/code&gt; as a parameter; this is to "get the accumulator going" in the right direction. If you're adding/manipulating numbers, it's a good idea to start at &lt;code&gt;0&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;As an example, it'll look like this:&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;// This is incredibly similar to the MDN example&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;arrayToReduce&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="nx"&gt;arrayToReduce&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;reduce&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;prevValue&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;currValue&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;prevValue&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;currValue&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But let's take MDN's example and break it down a bit. Complete with console.logs to make a bit more sense of how it works:&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;initialValue&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&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;arrayToReduce&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="nx"&gt;arrayToReduce&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;reduce&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;previousValue&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;currentValue&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;prev:&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;previousValue&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;curr:&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;currentValue&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;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;previousValue&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;currentValue&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;result&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;result&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;result&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="nx"&gt;initialValue&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Running this will output the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt; "prev: " 0 "curr:" 1
&amp;gt; "result" 1
&amp;gt; "prev: " 1 "curr:" 2
&amp;gt; "result" 3
&amp;gt; "prev: " 3 "curr:" 3
&amp;gt; "result" 6
&amp;gt; "prev: " 6 "curr:" 4
&amp;gt; "result" 10
&amp;gt; "prev: " 10 "curr:" 5
&amp;gt; "result" 15
&amp;gt; 15
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I feel like explaining this without an example showing the output and everything is disingenuous. As you can clearly see here, basically the &lt;code&gt;result&lt;/code&gt; is added to the next iteration as an input in the callback (&lt;code&gt;previousValue&lt;/code&gt;), and the current iteration that the array is looping through, is the &lt;code&gt;currentValue&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This is basically the equivalent of doing:&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;arrayToForLoop&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&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;valueToKeepTrackOf&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;//equivalent to reduce's "initialValue" parameter&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;finalResult&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[];&lt;/span&gt;

&lt;span class="nx"&gt;arrayToForLoop&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;forEach&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;item&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;prev:&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;valueToKeepTrackOf&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;current:&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;valueToKeepTrackOf&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="nx"&gt;item&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;valueToKeepTrackOf&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
  &lt;span class="nx"&gt;finalResult&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;valueToKeepTrackOf&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="nx"&gt;finalResult&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So there you have it! That's &lt;code&gt;reduce&lt;/code&gt; in a nutshell. It's incredibly useful for appending/using operators on! &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;My personal advice&lt;/strong&gt;: Don't use this for complex things; if you need to do something really complex or nested in a bunch of JSON objects, best to simplify it first and go from there.&lt;/p&gt;

&lt;p&gt;As always, &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce" rel="noopener noreferrer"&gt;MDN is a great source to understand this in its entirety&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Cover Photo Credit: &lt;a href="https://www.lacucinaitaliana.com/italian-food/how-to-cook/how-to-make-a-red-wine-reduction?refresh_ce=" rel="noopener noreferrer"&gt;La Cucina Italia&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>🎶 Promise Me No Promises</title>
      <dc:creator>Patrick Canella</dc:creator>
      <pubDate>Wed, 27 Jul 2022 20:48:00 +0000</pubDate>
      <link>https://forem.com/pcanella/promise-me-no-promises-2gea</link>
      <guid>https://forem.com/pcanella/promise-me-no-promises-2gea</guid>
      <description>&lt;h2&gt;
  
  
  &lt;code&gt;🎶 Promise Me No Promises 🎶&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;(okay, I'm done...)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;In JS what are they? If you're a beginner in JS you already should have &lt;em&gt;some&lt;/em&gt; knowledge of callbacks and how they work. If not let me know and I can make a blog post on these as well. &lt;/p&gt;

&lt;h2&gt;
  
  
  Anyway, what is a Promise?
&lt;/h2&gt;

&lt;p&gt;Well, &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" rel="noopener noreferrer"&gt;MDN defines a Promise&lt;/a&gt; as:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The Promise object represents the eventual completion (or failure) of an asynchronous operation and its resulting value.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Ok, those are some words. Let me give an example of a basic &lt;code&gt;Promise&lt;/code&gt; and then kind of go over what it means.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbegss8rkfvbzdl5hnm1x.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbegss8rkfvbzdl5hnm1x.png" alt="const somePromise = new Promise((resolve, reject) =&amp;gt; {&amp;lt;br&amp;gt;
    const someResult = 'hello there!';&amp;lt;br&amp;gt;
    resolve(someResult);&amp;lt;br&amp;gt;
});somePromise.then(resultArg =&amp;gt; {&amp;lt;br&amp;gt;
    console.log(resultArg);&amp;lt;br&amp;gt;
});" width="800" height="572"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is a really basic promise. Basically what we are doing here is saying "hey when this promise gets &lt;code&gt;resolved&lt;/code&gt;, we are going to print out &lt;code&gt;hello there&lt;/code&gt;. Easy, right? But why would I use a Promise for this, when I can just do:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;console.log('hello there!')&lt;/code&gt;?&lt;/p&gt;

&lt;p&gt;Great point! &lt;code&gt;Promises&lt;/code&gt; aren't really needed or used for something like this. So what are they used for? &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Promises are used to wrap asynchronous functions (like HTTP requests) and give its result in a synchronous manner&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Yes yes, we all know that JS is asynchronous, but if we don't have something like promises, we'll be doing what in the JS community we like to call 🔥🔥 &lt;strong&gt;CALLBACK HELL&lt;/strong&gt; 🔥🔥. Callback hell is when you are calling many functions whose results come in the form of a callback with an argument as a value... something like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fz2c67ipkxseirh1mcsbv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fz2c67ipkxseirh1mcsbv.png" alt=" " width="800" height="624"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;YIKES.&lt;/strong&gt; What a mess. This is how we used to have to write JS! This simplified version below uses Promises.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgyft3k149048jg8v9984.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgyft3k149048jg8v9984.png" alt=" " width="800" height="551"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see &lt;strong&gt;we wrap the existing async &lt;code&gt;fs.readdir&lt;/code&gt; function in a Promise and once &lt;code&gt;fs.readdir&lt;/code&gt; has completed, we get the result via &lt;code&gt;resolve&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;BTW, &lt;code&gt;resolve&lt;/code&gt; is just a fancy way of using &lt;code&gt;return&lt;/code&gt; in a &lt;code&gt;Promise&lt;/code&gt;. It basically says "hey here's the data you are looking for, I'm going to return this in the callback. &lt;code&gt;reject&lt;/code&gt; is similar, but will only print out the &lt;code&gt;err&lt;/code&gt; when the you put a &lt;code&gt;.catch&lt;/code&gt; method at the end of the chain. Let's take a look at what that would look like: &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdjr3wq6r4098owme8rvm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdjr3wq6r4098owme8rvm.png" alt=" " width="800" height="588"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  That's great, but what's a real world example of this?
&lt;/h2&gt;

&lt;p&gt;Great question! In JS, we have the ever famous &lt;code&gt;fetch&lt;/code&gt; function (both on &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API" rel="noopener noreferrer"&gt;the browser&lt;/a&gt; and now on &lt;a href="https://blog.logrocket.com/fetch-api-node-js/" rel="noopener noreferrer"&gt;NodeJS!&lt;/a&gt;), which is a PERFECT use of Promises! Let's see how.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbr53ekoeuirzquxshzc0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbr53ekoeuirzquxshzc0.png" alt=" " width="800" height="419"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So what just happened? A few things, as mentioned in the code comments:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;We initially make the &lt;code&gt;fetch&lt;/code&gt; call with the URL as its parameter. This kicks off an &lt;code&gt;async&lt;/code&gt; function that will return a &lt;code&gt;Promise&lt;/code&gt; and "resolves" as a &lt;code&gt;Response&lt;/code&gt; type (more on that &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Response" rel="noopener noreferrer"&gt;here&lt;/a&gt;).&lt;/li&gt;
&lt;li&gt;Once that &lt;code&gt;fetch&lt;/code&gt; completes or &lt;code&gt;resolves&lt;/code&gt;, we want to use &lt;code&gt;then&lt;/code&gt; to get the &lt;code&gt;response&lt;/code&gt; object.&lt;/li&gt;
&lt;li&gt;With the &lt;code&gt;response&lt;/code&gt; object, we now want to make this response into a JSON format. Thus, we call &lt;code&gt;response.json()&lt;/code&gt;. We'll set this to a variable, so we can use it in the next line.&lt;/li&gt;
&lt;li&gt;Once &lt;code&gt;response.json()&lt;/code&gt; has completed, we use the final &lt;code&gt;then&lt;/code&gt; to print out the JSON &lt;code&gt;body&lt;/code&gt; that we are looking for.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;and ta da! You get this as a result:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhg5wcdg42iyrn8470dq4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhg5wcdg42iyrn8470dq4.png" alt="carbon (3)" width="800" height="536"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Honestly, this is broken down a bit much. Usually for a simple &lt;code&gt;fetch&lt;/code&gt; request, you can shorten it with chaining like so:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr4cx5ay7h3i3t5or8q4e.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr4cx5ay7h3i3t5or8q4e.png" alt="carbon (2)" width="800" height="324"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And that's it! Promises provide a great alternative to Callback hell and are great for async functions of any kind.&lt;/p&gt;

&lt;h2&gt;
  
  
  Actually, one more thing...
&lt;/h2&gt;

&lt;p&gt;With ES6, the introduction of &lt;code&gt;async&lt;/code&gt;/&lt;code&gt;await&lt;/code&gt; has made Promises even easier than before! I'll probably do a separate blog post on this, but in essence instead of doing:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flmivc5685w575cugq8zt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flmivc5685w575cugq8zt.png" alt="carbon (1)" width="800" height="363"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can simply do:&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F593gkj1k5nhyctfucphf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F593gkj1k5nhyctfucphf.png" alt="carbon" width="800" height="279"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;No &lt;code&gt;then&lt;/code&gt; needed! This is some nice syntactic sugar that JS provides now so we can write our code more linearly like many synchronous languages can do. It's incredibly useful and you'll see most promises written in this way.&lt;/p&gt;

&lt;p&gt;Anyway, I hope this helps you understand how promises work. Let me know if you have any questions! Thanks for reading 😄&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Old blog post archive</title>
      <dc:creator>Patrick Canella</dc:creator>
      <pubDate>Wed, 16 Sep 2020 03:26:11 +0000</pubDate>
      <link>https://forem.com/pcanella/old-blog-post-archive-4l41</link>
      <guid>https://forem.com/pcanella/old-blog-post-archive-4l41</guid>
      <description>&lt;p&gt;Hello all! In case you dropped here from my website, here are a bunch of old blog posts I did years ago! I wanted to put them here one by one, but I'd rather spend time creating new content than porting over old stuff, so here's a link to it:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;a href="https://pcanella.github.io/blog/" rel="noopener noreferrer"&gt;BLOG ARCHIVES&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>The Vimeo API; Or: Why postMessage rocks!</title>
      <dc:creator>Patrick Canella</dc:creator>
      <pubDate>Wed, 16 Sep 2020 03:11:03 +0000</pubDate>
      <link>https://forem.com/pcanella/the-vimeo-api-or-why-postmessage-rocks-120e</link>
      <guid>https://forem.com/pcanella/the-vimeo-api-or-why-postmessage-rocks-120e</guid>
      <description>&lt;p&gt;Transferring this over from my old blog to DEV.to... bear with me for any aberrations!&lt;/p&gt;

&lt;p&gt;m going to try to keep this post short and sweet; because: A) It’s a quick overview B) Nobody reads the “fluff” of coding articles (because we’re usually looking for answers!)&lt;/p&gt;

&lt;p&gt;Anyway, this post is to highlight the &lt;a href="https://developer.vimeo.com/player/js-api" rel="noopener noreferrer"&gt;Vimeo JS API&lt;/a&gt;, it’s optional &lt;a href="https://github.com/vimeo/player-api/tree/master/javascript" rel="noopener noreferrer"&gt;Froogaloop helper library&lt;/a&gt; (which I actually did NOT use) and creating a quick wrapper dedicated to Vimeo’s basic API functionality.&lt;/p&gt;

&lt;p&gt;For this project, we needed to use both the YouTube and Vimeo APIs to take control of their iframes. The weird thing is, both of these companies decided to implement their API quite differently.&lt;/p&gt;

&lt;p&gt;If you’re debating whether or not to use Vimeo or YouTube, I would actually suggest Vimeo, only because their API is a lot more straightforward (in my opinion) and it’s easier to get up and running with it. YouTube’s iframe API is sort of clunky and you have to hack a bit to get it working how you want. You also MUST include their JS, which is actually really irritating if you want multiple instances.&lt;/p&gt;

&lt;p&gt;ANYWAY, let’s get on with it.&lt;/p&gt;

&lt;p&gt;Now, Vimeo’s iframe API revolves around the really cool (and fairly recent) postMessage API that allows us to send events to a 3rd party iframe where they have their own event listeners! A really cool (and secure) way to execute cross-domain requests. We can actually set this up quite easily with Vimeo’s API!&lt;/p&gt;

&lt;p&gt;First, you’ll want to make sure that your embedded url looks something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;iframe&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"iframe_video"&lt;/span&gt; &lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"https://player.vimeo.com/video/&amp;lt;VIDEO_ID&amp;gt;?byline=0&amp;amp;portrait=0&amp;amp;title=0&amp;amp;autoplay=0&amp;amp;badge=0&amp;amp;api=1&amp;amp;player_id=iframe_video"&lt;/span&gt;&lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I have some fancy parameters here (which are optional and &lt;a href="https://developer.vimeo.com/player/embedding#universal-parameters" rel="noopener noreferrer"&gt;can be found here&lt;/a&gt;), but the most important ones are api=1 (which enables the postMessage/JS API) and the player_id (which says “ok, I’ll correspond with the iframe with ID of “iframe_video”)&lt;/p&gt;

&lt;p&gt;Let’s take a look at a quick code sample (as a full reference, here’s &lt;del&gt;&lt;a href="https://github.com/pcanella/pcanella.github.io/blob/master/_codesnippets/vimeo_wrapper.js" rel="noopener noreferrer"&gt;the wrapper I have created&lt;/a&gt;&lt;/del&gt; &lt;em&gt;I have learned recently that this is out of date and no longer works, sadly&lt;/em&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="nx"&gt;post&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;action&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;value&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;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;method&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;action&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="nx"&gt;$player&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;iframe_video&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="c1"&gt;// Just as a check to make sure value exists&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;value&lt;/span&gt;&lt;span class="p"&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="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;value&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;url&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;$player&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;src&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="nx"&gt;message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;stringify&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;$player&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;contentWindow&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;postMessage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;url&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;Ok, so there’s a bit we should go over.&lt;/p&gt;

&lt;p&gt;params:&lt;/p&gt;

&lt;p&gt;-&lt;code&gt;action&lt;/code&gt; (required): the action you want Vimeo API to execute.&lt;a href="https://developer.vimeo.com/player/js-api#events" rel="noopener noreferrer"&gt;Need a list? Here you go&lt;/a&gt;!&lt;/p&gt;

&lt;p&gt;-&lt;code&gt;value&lt;/code&gt; (optional depending on action): The value that is attributed to the action (for example, the action would be “seek” and the value would be a JSON object like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
 'seconds:'192.622',
 "percent":'0.527',
 "duration":'365.507'
 }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;(BTW this is totally taken from the API docs)&lt;/p&gt;

&lt;p&gt;Anyway, this is all fairly straight forward if you’ve used postMessage. Haven’t used postMessage? &lt;a href="http://davidwalsh.name/demo/window-post-message.php" rel="noopener noreferrer"&gt;Check out David Walsh’s example&lt;/a&gt;!&lt;/p&gt;

&lt;p&gt;Below is a quick demo of Vimeo’s postMessage API working in action :) (want to see the code behind it? &lt;a href="http://jsfiddle.net/rahurrty/3/" rel="noopener noreferrer"&gt;Check out the jsFiddle&lt;/a&gt;)&lt;/p&gt;

&lt;p&gt;&lt;em&gt;This post is from September 2015 and may not be up-to-date, however I am transferring to Dev.to for historical reasons&lt;/em&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Is this a blog or something?</title>
      <dc:creator>Patrick Canella</dc:creator>
      <pubDate>Wed, 16 Sep 2020 03:04:39 +0000</pubDate>
      <link>https://forem.com/pcanella/is-this-a-blog-or-something-3pnj</link>
      <guid>https://forem.com/pcanella/is-this-a-blog-or-something-3pnj</guid>
      <description>&lt;p&gt;Hello all! &lt;/p&gt;

&lt;p&gt;I'm not quite sure what DEV.to exactly is, but I have decided to use it as some sort of blogging tool and somewhere I can share my inane thoughts and opinions about the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Javascript&lt;/li&gt;
&lt;li&gt;Swift&lt;/li&gt;
&lt;li&gt;Tech in general&lt;/li&gt;
&lt;li&gt;IoT Stuff (my current personal favorite ATM)&lt;/li&gt;
&lt;li&gt;Retro Macs &amp;amp; Restoration!&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I have no idea what audience I will reach with this account, but I am willing to start!&lt;/p&gt;

&lt;p&gt;If you're interested in rants, please follow &lt;a href="https://www.twitter.com/pcanella" rel="noopener noreferrer"&gt;@pcanella on Twitter&lt;/a&gt;.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
