<?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: LuBustos</title>
    <description>The latest articles on DEV Community by LuBustos (@lubustos).</description>
    <link>https://dev.to/lubustos</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%2F729688%2F5f9dd1cb-666a-48a0-b937-ec25353a5a30.jpeg</url>
      <title>DEV Community: LuBustos</title>
      <link>https://dev.to/lubustos</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/lubustos"/>
    <language>en</language>
    <item>
      <title>How to make a request within a loop</title>
      <dc:creator>LuBustos</dc:creator>
      <pubDate>Thu, 27 Oct 2022 10:51:00 +0000</pubDate>
      <link>https://dev.to/lubustos/how-to-make-a-request-within-a-loop-585a</link>
      <guid>https://dev.to/lubustos/how-to-make-a-request-within-a-loop-585a</guid>
      <description>&lt;p&gt;Hey! This is my first blog here, so I hope you enjoy it :p&lt;/p&gt;

&lt;h2&gt;
  
  
  Let's go
&lt;/h2&gt;

&lt;p&gt;There are two ways to make a request within a loop: we have the &lt;strong&gt;sequence&lt;/strong&gt; way or the &lt;strong&gt;parallel&lt;/strong&gt; way.&lt;/p&gt;

&lt;p&gt;I made a small example in codesandbox to show a practical case.&lt;/p&gt;

&lt;p&gt;Here is the link: &lt;a href="https://codesandbox.io/s/fast-glitter-ce6yk9"&gt;Example&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;Sequence method&lt;/em&gt;&lt;/strong&gt;:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const sequence = async () =&amp;gt; {
  try {
    for (const color of colors) {
      const response = await save(color);
      console.log("response sequence", response);
    }
  } catch (error) {
    console.error(error);
  }
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this way we have to use a &lt;code&gt;for...of&lt;/code&gt; to loop every item of the array and to execute the method save within the loop.&lt;br&gt;
In this case the code will be executed line by line, which means first the method save and then the &lt;code&gt;console.log&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;And this is the result:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;saved it {id: 1, color: "red"}
response sequence {id: 1, color: "red"}
saved it {id: 2, color: "green"}
response sequence {id: 2, color: "green"}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This way is a little bit slow, but if you are waiting for a response from the server, such as an id, it is useful.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;Parallel method&lt;/em&gt;&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const parallel = async () =&amp;gt; {
  try {
    await Promise.all(
      colors.map(async (color) =&amp;gt; {
        const response = await save(color);
        console.log("response parallel", response);
      })
    );
  } catch (error) {
    console.error(error);
  }
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this case we are using a &lt;code&gt;Promise.all&lt;/code&gt; within a &lt;code&gt;.map&lt;/code&gt;. This is &lt;strong&gt;important&lt;/strong&gt; because this method returns an array of promises and the &lt;code&gt;Promise.all&lt;/code&gt; will execute all the promises together.&lt;/p&gt;

&lt;p&gt;Looping the array, every element in it will enter the save method and, after this, the next line will be executed, in this case the &lt;code&gt;console.log&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This is the result:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;saved it {id: 1, color: "red"}
saved it {id: 2, color: "green"}
response parallel {id: 1, color: "red"}
response parallel {id: 2, color: "green"}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can appreciate better from the link that I attached or maybe creating a file.js and debugging step by step.&lt;/p&gt;

&lt;p&gt;This is all for today.&lt;br&gt;
I hope you can find this useful and I will be expecting your comments.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Dos maneras de hacer una request dentro de un loop</title>
      <dc:creator>LuBustos</dc:creator>
      <pubDate>Thu, 27 Oct 2022 10:50:52 +0000</pubDate>
      <link>https://dev.to/lubustos/dos-maneras-de-hacer-una-request-dentro-de-un-loop-41k4</link>
      <guid>https://dev.to/lubustos/dos-maneras-de-hacer-una-request-dentro-de-un-loop-41k4</guid>
      <description>&lt;p&gt;¡Hola! Primera vez creando un post aquí, vamos a ver cómo sale :p&lt;/p&gt;

&lt;h2&gt;
  
  
  ¡Empecemos!
&lt;/h2&gt;

&lt;p&gt;Existen dos maneras de hacer una request dentro de un loop y estas son de manera &lt;strong&gt;paralela&lt;/strong&gt; o &lt;strong&gt;secuencial&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Armé un pequeño ejemplo dentro de un codesandbox para que el ejemplo sea mas práctico.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://codesandbox.io/s/fast-glitter-ce6yk9"&gt;Ejemplo&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;Método secuencial:&lt;/em&gt;&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const sequence = async () =&amp;gt; {
  try {
    for (const color of colors) {
      const response = await save(color);
      console.log("response sequence", response);
    }
  } catch (error) {
    console.error(error);
  }
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Para realizar una request de manera secuencial lo que tenemos que usar es un &lt;code&gt;for...of&lt;/code&gt; que recorra el objeto en cuestión y, dentro del loop, la petición al backend.&lt;br&gt;
En este caso el código se va a ejecutar línea por línea.&lt;br&gt;
Primero ingresará al método save, nos mostrará el objeto guardado y retornará la promesa para luego (en la siguiente línea dentro del for) mostrarnos la response.&lt;/p&gt;

&lt;p&gt;Este es el resultado:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;saved it {id: 1, color: "red"}
response sequence {id: 1, color: "red"}
saved it {id: 2, color: "green"}
response sequence {id: 2, color: "green"}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Es un método un poco lento, pero si necesitamos utilizar los datos que nos trae del servidor luego de ejecutar la petición, como por ejemplo un id, es bastante útil.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;Método Paralelo:&lt;/em&gt;&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const parallel = async () =&amp;gt; {
  try {
    await Promise.all(
      colors.map(async (color) =&amp;gt; {
        const response = await save(color);
        console.log("response parallel", response);
      })
    );
  } catch (error) {
    console.error(error);
  }
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;En este caso estamos utilizando un &lt;code&gt;Promise.all&lt;/code&gt; y dentro un &lt;code&gt;.map&lt;/code&gt;, esto es &lt;strong&gt;importante&lt;/strong&gt; debido a que este método &lt;strong&gt;retorna un array&lt;/strong&gt;, por lo que en este caso retornará las responses (en forma de promises) y el &lt;code&gt;Promise.all&lt;/code&gt; las ejecutará de manera inmediata todas juntas.&lt;/p&gt;

&lt;p&gt;Recorriendo el array cada elemento ingresará al método save y una vez que todos los elementos del array realizaron la petición, en ese momento se va a ejecutar a la siguiente línea de la misma manera que la anterior, por lo tanto cada elemento del array ejecutará el &lt;code&gt;console.log&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Y este es el resultado:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;saved it {id: 1, color: "red"}
saved it {id: 2, color: "green"}
response parallel {id: 1, color: "red"}
response parallel {id: 2, color: "green"}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Esto se puede apreciar mejor desde el link que adjunté o pueden pasar el código a un file.js y debuggearlo paso por paso.&lt;/p&gt;

&lt;p&gt;¡Eso es todo por hoy!&lt;br&gt;
Espero que les haya gustado y leo sus comentarios.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>programming</category>
      <category>tutorial</category>
      <category>spanish</category>
    </item>
  </channel>
</rss>
