<?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: Marina Costa</title>
    <description>The latest articles on DEV Community by Marina Costa (@marinafroes).</description>
    <link>https://dev.to/marinafroes</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%2F399507%2Fc8a4d431-fe99-4197-a37d-cde6ac9a2210.jpg</url>
      <title>DEV Community: Marina Costa</title>
      <link>https://dev.to/marinafroes</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/marinafroes"/>
    <language>en</language>
    <item>
      <title>Illustrated JS: Promises</title>
      <dc:creator>Marina Costa</dc:creator>
      <pubDate>Tue, 11 Aug 2020 19:46:42 +0000</pubDate>
      <link>https://dev.to/marinafroes/illustrated-js-promises-5ed7</link>
      <guid>https://dev.to/marinafroes/illustrated-js-promises-5ed7</guid>
      <description>&lt;p&gt;&lt;em&gt;While learning JavaScript and programming in general, I find it useful to create some analogies to understand the concepts better. Here I will share an analogy about Promises in JavaScript. Feel free to contribute in the comments.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;In Javascript, if we want to call a function only after an asynchronous function returns, we can use &lt;strong&gt;callbacks&lt;/strong&gt;. Let's say I'm baking a cake with chocolate icing, in that case, my &lt;code&gt;bakeCake()&lt;/code&gt; function is asynchronous and takes &lt;code&gt;prepareIcing()&lt;/code&gt; (that's the callback) as an argument, calling it only when the cake is completely baked.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--9nty7OMd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/3ury9hcftllr91ye6h7k.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--9nty7OMd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/3ury9hcftllr91ye6h7k.jpg" alt="Drawing of a baker responsible for baking the cake, calling the one responsible for preparing the icing" width="50%"&gt;&lt;/a&gt;&lt;br&gt;Fig.1 - The baker responsible for the baking cake action is &lt;strong&gt;calling back&lt;/strong&gt; the function responsible for the icing preparation after the baking is finished
  &lt;/p&gt;

&lt;p&gt;But in this article we're focusing on using &lt;strong&gt;Promises&lt;/strong&gt;. So how promises can help solve this problem? A promise is an object that wraps an asynchronous action and can be &lt;strong&gt;fulfilled&lt;/strong&gt; with a value if the operation is successful or &lt;strong&gt;rejected&lt;/strong&gt; with a reason, if it fails. This way I'm able to add a handler for a value (or a reason) that I will have only at some point in the future. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Ji2cSovY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/rh1iefz5cshdhz0vpud6.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Ji2cSovY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/rh1iefz5cshdhz0vpud6.jpg" alt="Drawing of a baker saying: Sounds complicated, right? Let's try again with a simple cake" width="65%"&gt;&lt;/a&gt;&lt;br&gt;Fig.2 - My baker likes to use cakes to explain stuff
  &lt;/p&gt;

&lt;p&gt;Going back to the baking example, I can say my oven is a &lt;em&gt;promise of a cake&lt;/em&gt;, because it wraps my baking cake action and starts with a &lt;em&gt;pending&lt;/em&gt; state (it's still baking). But I have a smart oven that will tell me when the action finishes. If the baking was successful, it's &lt;em&gt;fulfilled&lt;/em&gt; with a cake. Otherwise, it's &lt;em&gt;rejected&lt;/em&gt; with a burned cake or any other reason for the failure.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--m-UJegNl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/u88p9gsq2rz0i77ose50.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--m-UJegNl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/u88p9gsq2rz0i77ose50.jpg" alt="Drawing of an oven and the possible states for the cake promise" width="70%"&gt;&lt;/a&gt;&lt;br&gt;Fig.3 - The oven is a promise of a cake that starts with a pending state and let's me know when it's fulfilled (success cake) or rejected (failure cake)
  &lt;/p&gt;

&lt;p&gt;Like we said before, using promises, we can add handlers for its possible states when it settles (it is either fulfilled or rejected). The &lt;code&gt;.then()&lt;/code&gt; method can handle both success and failure (less common), and the &lt;code&gt;.catch()&lt;/code&gt; method only can handle failure.&lt;/p&gt;

&lt;p&gt;This is how I would handle a baking simple cake (no icing) action: if it is successful, &lt;strong&gt;then&lt;/strong&gt; I can serve the cake. But if I burn my cake, I can &lt;strong&gt;catch&lt;/strong&gt; the failed cake, throw it in the garbage and order a pie for my dessert instead. 😂 &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--lZOQYGaO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/m1b7e6p62im1mi5cc1ej.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--lZOQYGaO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/m1b7e6p62im1mi5cc1ej.jpg" alt="Drawing of an oven and the possible states for the cake promise, including handler for success (serveCake()) and failure (orderPie())" width="80%"&gt;&lt;/a&gt;&lt;br&gt;Fig.4 - If cakeBake() is fulfilled, &lt;strong&gt;then&lt;/strong&gt; call the success handler, the serveCake() function. Otherwise, we &lt;strong&gt;catch&lt;/strong&gt; the reason (burned cake) for the rejection and call the failure handler, the orderPie() function
  &lt;/p&gt;

&lt;p&gt;Another thing to keep in mind is that, since &lt;strong&gt;one promise returns another promise&lt;/strong&gt;, they can be chained. In my previous cake with icing example, I can finish baking my cake, &lt;em&gt;then&lt;/em&gt;  prepare the icing, &lt;em&gt;then&lt;/em&gt; with both the cake and the icing ready, cover the cake. In that case, if any of the promises in the chain fails, I can handle all the rejections with only one &lt;em&gt;catch&lt;/em&gt; and order a pie.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--TetFrwfU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/msussw6yh1hznczlydl7.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--TetFrwfU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/msussw6yh1hznczlydl7.jpg" alt="Drawing of chained promises and handlers for success and failure"&gt;&lt;/a&gt;&lt;br&gt;Fig.5 - Promises chained with .then() and catch() to handle successes and failures
  &lt;/p&gt;

&lt;p&gt;But there's a better way to do what we did in the last example. My &lt;code&gt;bakeCake()&lt;/code&gt; and my &lt;code&gt;prepareIcing()&lt;/code&gt; functions are independent, which means they can happen at the same time. For that, we can use &lt;code&gt;Promise.all()&lt;/code&gt; to wait for both results (cake and icing) before executing the &lt;code&gt;coverCake()&lt;/code&gt; function.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--klp0Pm2l--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/4y4t4ee1dv0y2potjy6y.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--klp0Pm2l--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/4y4t4ee1dv0y2potjy6y.jpg" alt="Drawing of promise all example"&gt;&lt;/a&gt;&lt;br&gt;Fig.6 - Promise.all() handles both the promise of the cake and the promise of the icing before covering the cake
  &lt;/p&gt;

&lt;p&gt;Inside &lt;code&gt;Promise.all()&lt;/code&gt;, if &lt;em&gt;any of the promises&lt;/em&gt; rejects (fails), the entire promise is rejected. Which makes sense, right? I can't cover the cake if either the cake or the icing is missing.&lt;/p&gt;

&lt;p&gt;This was a very simplified and summarised explanation of the concept of Promise, and the use of &lt;code&gt;.then()&lt;/code&gt;, &lt;code&gt;.catch()&lt;/code&gt; and &lt;code&gt;Promise.all()&lt;/code&gt; methods. I hope it was clear enough.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Thanks for reading, feel free to contribute in the comments. I promise my next article is already in the oven right now (pending). And eventually, I'll share it on &lt;a href="https://twitter.com/facosta_marina"&gt;my twitter&lt;/a&gt; as soon as it's fulfilled or I'll post pictures of cats if it's rejected.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Bullet points for this article:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Promises are objects wrapping asynchronous actions. &lt;/li&gt;
&lt;li&gt;Promises start with a &lt;em&gt;pending&lt;/em&gt; state, and at some point in the future they settle to a &lt;em&gt;fulfilled&lt;/em&gt; (success) or &lt;em&gt;rejected&lt;/em&gt; (failure) state.&lt;/li&gt;
&lt;li&gt;A promise returns a new promise that can be used for chaining.&lt;/li&gt;
&lt;li&gt;We can add handlers to the success value or the failure reason once the action returns, by using &lt;code&gt;.then()&lt;/code&gt; or &lt;code&gt;.catch()&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Promise.all()&lt;/code&gt; waits for all the promises to be resolved. If any rejects, it's rejected.&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;References and recommended further reading&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://dev.to/marinafroes/illustrated-js-javascript-asynchronous-behaviour-1akc"&gt;Illustrated JS: JavaScript asynchronous behaviour&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://eloquentjavascript.net/11_async.html"&gt;Asynchronous programing&lt;/a&gt; in Eloquent JavaScript by Marijn Haverbeke&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises"&gt;Using promises&lt;/a&gt; in MDN Web Docs&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>codenewbie</category>
      <category>javascript</category>
      <category>beginners</category>
      <category>learning</category>
    </item>
    <item>
      <title>Illustrated JS: JavaScript asynchronous behaviour</title>
      <dc:creator>Marina Costa</dc:creator>
      <pubDate>Thu, 06 Aug 2020 10:51:00 +0000</pubDate>
      <link>https://dev.to/marinafroes/illustrated-js-javascript-asynchronous-behaviour-1akc</link>
      <guid>https://dev.to/marinafroes/illustrated-js-javascript-asynchronous-behaviour-1akc</guid>
      <description>&lt;p&gt;&lt;em&gt;While learning JavaScript and programming in general, I find it useful to take notes and create some analogies to understand the concepts. Here I will share some of my notes about the asynchronous behaviour of JavaScript and the fact it’s single threaded. Feel free to correct me if I understood something wrong.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;JavaScript has only one thread running for each process. I think about it as a kitchen where only one baker is in charge for the process of baking a cake with chocolate icing.&lt;/p&gt;

&lt;p&gt;The baker is the CPU and they would have to execute one  subprocess at a time: baking the cake and preparing the icing. After that, the same person would also have to cover the cake with the icing.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fmemzx8zzkngbqkfcys4f.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fmemzx8zzkngbqkfcys4f.jpg" alt="Drawing of a baker executing all steps for the cake process"&gt;&lt;/a&gt;&lt;br&gt;Fig.1 - One baker (CPU) doing one subprocess at a time (single thread)
  &lt;/p&gt;

&lt;p&gt;If it was multithreaded we could have only one CPU executing different subprocesses at the same time (baker with more arms).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Ffcjx9fe2gmeuwow92e13.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Ffcjx9fe2gmeuwow92e13.jpg" alt="Drawing of a baker with multiple arms doing different tasks"&gt;&lt;/a&gt;&lt;br&gt;Fig.2 - One baker (CPU) doing multiple subprocesses at the same time (multithreads)
  &lt;/p&gt;

&lt;p&gt;Or we could have more CPUs (bakers) working in different subprocesses: cake preparation, icing preparation and cake covering. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Ffjk2qjqf9c3tulnvli0w.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Ffjk2qjqf9c3tulnvli0w.jpg" alt="Drawing of 3 bakers executing 1 step for the cake process each"&gt;&lt;/a&gt;&lt;br&gt;Fig.3 - Three bakers executing subprocess concurrently
  &lt;/p&gt;

&lt;p&gt;But in that case, the third subprocess (covering the cake) would need resources from the other two. So it would be necessary to add synchronisation to those actions, but let’s not focus on that right now. &lt;/p&gt;

&lt;p&gt;Although multithreading maximises the CPU time, it’s a little bit more complex because it's harder to handle the concurrency, and the results are unpredictable.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F788u3lyctw4bfn4sy5kp.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F788u3lyctw4bfn4sy5kp.jpg" alt="Drawing of the oven on fire, burnt icing and the third baker without cake or icing"&gt;&lt;/a&gt;&lt;br&gt;Fig.4 - Unpredictable results in multithreading (absolutely exaggerated) 
  &lt;/p&gt;

&lt;p&gt;It's also important to mention that JavaScript is single threaded but it’s not synchronous. It means it doesn't need to completely finish one task before starting another one. &lt;/p&gt;

&lt;p&gt;If that was the case, my baker would have to keep waiting for the oven to finish baking the cake, for instance, before being able to start the icing preparation.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F3p5kxk9fzfhlk893owfd.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F3p5kxk9fzfhlk893owfd.jpg" alt="Drawing of a baker staring at the oven waiting for it to finish baking the cake"&gt;&lt;/a&gt;&lt;br&gt;Fig.5 - The baker is waiting for a slow action (baking the cake) to return before starting the icing preparation
  &lt;/p&gt;

&lt;p&gt;In fact, JavaScript has some resources that allows us to make it asynchronous, and in our cake example, the baker is able to start making the icing while they wait for the cake to be baked.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F2o9vaibjkn35gkzy061w.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F2o9vaibjkn35gkzy061w.jpg" alt="Drawing of a baker starting the icing preparation while the oven bakes the cake"&gt;&lt;/a&gt;&lt;br&gt;Fig.6 - Starting the icing preparation without waiting for the cake preparation action to be finished
  &lt;/p&gt;

&lt;p&gt;But let’s say our baker finishes the icing and the cake isn’t baked yet. Without the cake, they would not be able to move to the next step, which is covering the cake with the icing. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F7y0ukl7c7mtxeb2s2662.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F7y0ukl7c7mtxeb2s2662.jpg" alt="Drawing of a baker with the icing but no cake"&gt;&lt;/a&gt;&lt;br&gt;Fig.7 - The icing is finished before the cake, but the baker can't do the next task (covering the cake)
  &lt;/p&gt;

&lt;p&gt;Many operations in JavaScript depend on external responses like interactions with APIs or network communications and we don’t know for sure when we are going to receive the result of those actions.&lt;/p&gt;

&lt;p&gt;That's why we need to make sure that those dependent actions won’t be executed until we have access to the responses they need. For that, we can use promises and I'll talk about that on my next article. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F2fc5vrvq7encvz8d6kk9.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F2fc5vrvq7encvz8d6kk9.jpg" alt="Drawing of an oven with a cake baking inside and the subtitle: promise of a cake"&gt;&lt;/a&gt;&lt;br&gt;Fig.8 - A promise object wrapping an asynchronous action (baking the cake)
  &lt;/p&gt;

</description>
      <category>codenewbie</category>
      <category>javascript</category>
      <category>beginners</category>
      <category>learning</category>
    </item>
    <item>
      <title>How to get started with testing in React?</title>
      <dc:creator>Marina Costa</dc:creator>
      <pubDate>Wed, 29 Jul 2020 08:11:08 +0000</pubDate>
      <link>https://dev.to/marinafroes/how-to-get-started-with-testing-in-react-11mg</link>
      <guid>https://dev.to/marinafroes/how-to-get-started-with-testing-in-react-11mg</guid>
      <description>&lt;p&gt;Hello there, right now I'm focusing on learning testing in React, but I've been facing a hard time trying to find good material about that. Some tutorials that I find are too shallow or a little bit outdated and maybe I'm still too newbie to understand the documentation. &lt;/p&gt;

&lt;p&gt;What do you think? How did you get started with testing in React? Which resources do you recommend? (Bonus point if it's free or cheap)&lt;/p&gt;

</description>
      <category>discuss</category>
      <category>testing</category>
      <category>codenewbie</category>
    </item>
  </channel>
</rss>
