<?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: Mariane Santana</title>
    <description>The latest articles on DEV Community by Mariane Santana (@marianesantana).</description>
    <link>https://dev.to/marianesantana</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%2F320913%2Fd457a6fb-5863-45ae-b90f-54d66c3e7a17.png</url>
      <title>DEV Community: Mariane Santana</title>
      <link>https://dev.to/marianesantana</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/marianesantana"/>
    <language>en</language>
    <item>
      <title>Promises in 15 minutes</title>
      <dc:creator>Mariane Santana</dc:creator>
      <pubDate>Tue, 05 May 2020 21:20:58 +0000</pubDate>
      <link>https://dev.to/marianesantana/promises-in-15-minutes-9l7</link>
      <guid>https://dev.to/marianesantana/promises-in-15-minutes-9l7</guid>
      <description>&lt;p&gt;Good evening, fellows!&lt;/p&gt;

&lt;p&gt;Let's simplify the use of Promises?&lt;/p&gt;

&lt;p&gt;Particularly, when I started with the concept of Promises when I learned how to code, I saw a lot of online materials and tutorials that explained Promises in a very confusing way, then I decided to write a simple text explaining it in a very practical way. Of course, if you need to understand 'Promises Under the Hood' this article is not for you. If you need to understand Promises in a short period of time to make a solution, this is for you.&lt;/p&gt;

&lt;p&gt;Basically, Promises were made to create better asynchronous callback functions in Javascript, to make the code better organized. To grasp the concept think like it literally means that we are making a promise in real life. For example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;I promise that I will make you understand Promises in 15 minutes.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For that promise, I can do two things:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;I can either succeed by making you understand Promises in 15 minutes.&lt;/li&gt;
&lt;li&gt;Or I can fail and you'll not understand Promises in 15 minutes.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In code, it's the very same thing. Ok, so let's see this.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--sFgQvr-U--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/6uwp5fs41m8r13bquse1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--sFgQvr-U--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/6uwp5fs41m8r13bquse1.png" alt="Alt Text" width="800" height="642"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The output for this script is: &lt;code&gt;This is in then: success&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Here, we have a block inside of a Promise function that sums 1 + 1. If the result is 2, it means that our promise succeded, otherwise, means that our promise was rejected, because &lt;code&gt;1 + 1 = 2&lt;/code&gt;. If we change the number of the sum, we'll be rejected because we're saying that the variable of the sum is 2. If it isn't, promise rejected.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--zhaTpdxe--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/pxpr67vj2gks0r0a53kk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--zhaTpdxe--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/pxpr67vj2gks0r0a53kk.png" alt="Alt Text" width="800" height="642"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The output for this script is: &lt;code&gt;This is in catch: failed.&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now let's analyze this code&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--R7G3Dbn_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/dabk5oglz5tiugjhgwu9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--R7G3Dbn_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/dabk5oglz5tiugjhgwu9.png" alt="Alt Text" width="800" height="831"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This code sees if you're using Angular or Vue, if one of those is true, it calls a callback function which sends an alert with a title and a message.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--yQDsKQ0O--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/8q7s389fvwhwdnxknkxz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--yQDsKQ0O--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/8q7s389fvwhwdnxknkxz.png" alt="Alt Text" width="428" height="222"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now let's change this to a Promise and make this code better.&lt;/p&gt;

&lt;p&gt;First, we create a function that instantiates a Promise, passing our parameters &lt;strong&gt;resolve&lt;/strong&gt; and &lt;strong&gt;reject&lt;/strong&gt;. Then, we write the code that we want to be in that Promise, in my case, I want to ensure that developers are using the React lib. So I make the validation and dispatch the action that I want to execute when the promise resolves and when the promise rejects. Like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--MvOpUCyT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/x6lenxeizw9k2vmo6t5t.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--MvOpUCyT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/x6lenxeizw9k2vmo6t5t.png" alt="Alt Text" width="800" height="730"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After that, I write the &lt;strong&gt;then&lt;/strong&gt; function calling my Promise and I can do whatever I want in that block. When that Promise is completed, I want to log a message in my console both when I have a resolution or rejection. In the &lt;strong&gt;THEN&lt;/strong&gt; block is the code that runs when my promise is Resolved, and in the &lt;strong&gt;CATCH&lt;/strong&gt; block, the one which runs when my promise is rejected.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--SkVOZDRg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/232ejxq4vv3glryg8ewr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--SkVOZDRg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/232ejxq4vv3glryg8ewr.png" alt="Alt Text" width="800" height="399"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--xd14V9LY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/ihn3jhf1l975bq78kjd7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--xd14V9LY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/ihn3jhf1l975bq78kjd7.png" alt="Alt Text" width="425" height="203"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Nice, huh?&lt;/p&gt;

&lt;p&gt;Also, we can make simultaneous Promises using &lt;strong&gt;Promise.ALL&lt;/strong&gt; when we need to make two or more Promises at the same time.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--3P6wdpAa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/oagzxq3z26n402pe308l.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--3P6wdpAa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/oagzxq3z26n402pe308l.png" alt="Alt Text" width="800" height="517"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The output is going to be&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ruD4BzLR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/9a6v62chynfbio1ltfg9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ruD4BzLR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/9a6v62chynfbio1ltfg9.png" alt="Alt Text" width="425" height="192"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Or use &lt;strong&gt;Promise.RACE&lt;/strong&gt; if we need to get the result of the first Promise that executes and ignore the upcoming promises.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--o-nHumSB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/qwldsemeeecqkutwcpp9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--o-nHumSB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/qwldsemeeecqkutwcpp9.png" alt="Alt Text" width="800" height="517"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The output is going to be&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--mCOrRvtT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/o4u43mmvqpjvj6r2lvk1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--mCOrRvtT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/o4u43mmvqpjvj6r2lvk1.png" alt="Alt Text" width="429" height="163"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And I guess that's it!&lt;/p&gt;

&lt;p&gt;Some references:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://treinamento.nodebr.org/"&gt;https://treinamento.nodebr.org/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://braziljs.org/artigos/promises-no-javascript/"&gt;https://braziljs.org/artigos/promises-no-javascript/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/khaosdoctor/entendendo-promises-de-uma-vez-por-todas-44i7"&gt;https://dev.to/khaosdoctor/entendendo-promises-de-uma-vez-por-todas-44i7&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/khaosdoctor/construindo-uma-promise-do-zero-4ndp"&gt;https://dev.to/khaosdoctor/construindo-uma-promise-do-zero-4ndp&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://imasters.com.br/javascript/futuro-das-promises-no-javascript"&gt;https://imasters.com.br/javascript/futuro-das-promises-no-javascript&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.youtube.com/watch?v=wZwMVbgQZps&amp;amp;list=PLnOICPAPShyRZd7nnbC7h8kCQwM-6K3KW&amp;amp;index=10&amp;amp;t=0s"&gt;https://www.youtube.com/watch?v=wZwMVbgQZps&amp;amp;list=PLnOICPAPShyRZd7nnbC7h8kCQwM-6K3KW&amp;amp;index=10&amp;amp;t=0s&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Thank you!&lt;/p&gt;

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