<?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: Ananth Iyer</title>
    <description>The latest articles on Forem by Ananth Iyer (@anantsvc).</description>
    <link>https://forem.com/anantsvc</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%2F2004344%2Fd6672cf2-c559-4200-9f75-57e29a336233.png</url>
      <title>Forem: Ananth Iyer</title>
      <link>https://forem.com/anantsvc</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/anantsvc"/>
    <language>en</language>
    <item>
      <title>Understanding the composer why command</title>
      <dc:creator>Ananth Iyer</dc:creator>
      <pubDate>Wed, 30 Oct 2024 02:32:55 +0000</pubDate>
      <link>https://forem.com/anantsvc/understanding-the-composer-why-command-56k1</link>
      <guid>https://forem.com/anantsvc/understanding-the-composer-why-command-56k1</guid>
      <description>&lt;p&gt;Composer is a powerful dependency manager for PHP projects, simplifying the process of managing libraries and dependencies. One of its most useful commands is &lt;code&gt;composer why&lt;/code&gt;?, which helps developers understand why a particular package is included in their project.&lt;/p&gt;

&lt;p&gt;In this blog post, we'll explore how to it with examples from Magento 2 and Laravel.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is the composer why Command?
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;composer why&lt;/code&gt; command displays the dependency tree, showing which packages require a specific package and why it was added to the project. This is particularly useful for debugging and optimizing your project's dependencies.&lt;/p&gt;

&lt;h2&gt;
  
  
  Example: Magento 2
&lt;/h2&gt;

&lt;p&gt;Let's say you're working on a Magento 2 project and you want to know why the &lt;code&gt;kub-at/php-simple-html-dom-parser&lt;/code&gt; package is included. You can run the following command:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;composer why kub-at/php-simple-html-dom-parser&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This command will show you which package requires &lt;code&gt;kub-at/php-simple-html-dom-parser&lt;/code&gt; and why it was added to your Magento 2 project. For instance, it might reveal that the &lt;code&gt;kub-at/php-simple-html-dom-parser&lt;/code&gt; package is required by the &lt;code&gt;rosell-dk/dom-util-for-webp&lt;/code&gt; package, which is a dependency of your Magento 2 project.&lt;/p&gt;

&lt;h2&gt;
  
  
  Example: Laravel
&lt;/h2&gt;

&lt;p&gt;Similarly, in a Laravel project, you might want to understand why the &lt;code&gt;laravel/socialite&lt;/code&gt; package is included. You can run:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;composer why laravel/socialite&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This command will display the dependency tree, showing which package requires &lt;code&gt;laravel/socialite&lt;/code&gt; and why it was added to your Laravel project. For example, it might show that the &lt;code&gt;laravel/socialite&lt;/code&gt; package is required by the &lt;code&gt;laravel/ui&lt;/code&gt; package, which is a dependency of your Laravel project.&lt;/p&gt;

&lt;h2&gt;
  
  
  Benefits of Using composer why
&lt;/h2&gt;

&lt;p&gt;Using the &lt;code&gt;composer why&lt;/code&gt; command can help you:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Identify unnecessary dependencies: By understanding why a package is included, you can determine if it's still needed or if it can be removed to reduce bloat.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Optimize performance: Removing unnecessary dependencies can improve your project's performance.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Simplify debugging: Knowing the dependency tree can make it easier to troubleshoot issues related to package conflicts or missing dependencies.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;The &lt;strong&gt;composer why&lt;/strong&gt; command is a valuable tool for managing dependencies in PHP projects. By using it with examples from Magento 2 and Laravel, you can gain a deeper understanding of your project's dependencies and optimize your development workflow.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Unlocking Lazy Evaluation: Mastering JavaScript Generators</title>
      <dc:creator>Ananth Iyer</dc:creator>
      <pubDate>Fri, 30 Aug 2024 17:43:00 +0000</pubDate>
      <link>https://forem.com/anantsvc/unlocking-lazy-evaluation-mastering-javascript-generators-598n</link>
      <guid>https://forem.com/anantsvc/unlocking-lazy-evaluation-mastering-javascript-generators-598n</guid>
      <description>&lt;p&gt;This is my first post on Dev.to and excited to showcase JavaScript Generators with Fetch API.&lt;/p&gt;

&lt;p&gt;I have been coding JavaScript for long time and never heard of Generators in JS. What is it? Why to use? Where to use it?&lt;/p&gt;

&lt;p&gt;Recently, I show a YouTube video on it but still was confused to use it.&lt;/p&gt;

&lt;p&gt;I got deep dive into the Generators and it is used for lazy evaluation iterators in loops. Again I got confused because I never used huge array to iterator. Why to use it? 🤔&lt;/p&gt;

&lt;p&gt;I looked for real world use case of Generators and found it can be used with Fetch API for pagination or search etc.&lt;/p&gt;

&lt;p&gt;So let's code it&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;// Generator function to fetch paginated data&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nf"&gt;fetchPaginatedData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;apiUrl&lt;/span&gt;&lt;span class="p"&gt;)&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;page&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&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;hasMoreData&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&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;limit&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;while &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;hasMoreData&lt;/span&gt;&lt;span class="p"&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;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;apiUrl&lt;/span&gt;&lt;span class="p"&gt;}${&lt;/span&gt;&lt;span class="nx"&gt;page&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;res&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;())&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="o"&gt;=&amp;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;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Fetch error:&lt;/span&gt;&lt;span class="dl"&gt;'&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="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;page&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;limit&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nx"&gt;page&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nx"&gt;hasMoreData&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&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="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Function to handle the generator&lt;/span&gt;
&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;handleDataFetching&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;apiUrl&lt;/span&gt;&lt;span class="p"&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;dataIterator&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;fetchPaginatedData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;apiUrl&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;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;dataIterator&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;next&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

    &lt;span class="k"&gt;while &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&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;done&lt;/span&gt;&lt;span class="p"&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;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&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;value&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="s1"&gt;Fetched data:&lt;/span&gt;&lt;span class="dl"&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;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;dataIterator&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;next&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="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Usage&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;apiUrl&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;https://jsonplaceholder.typicode.com/posts/&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nf"&gt;handleDataFetching&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;apiUrl&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;In the above code:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Generator Function:&lt;/strong&gt; &lt;code&gt;fetchPaginatedData&lt;/code&gt; is a generator that yields a fetch request for each page of data.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Dynamic Iteration:&lt;/strong&gt; The while loop continues until it reached the limit of 10 page.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Handling the Generator:&lt;/strong&gt; &lt;code&gt;handleDataFetching&lt;/code&gt; function manages the generator, making asynchronous fetch calls and processing the data.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Browser support for Generators:&lt;/strong&gt; All major supports Chrome(v39), Firefox(v26), Safari (v10), Edge (v12), Opera (v26) and all mobile browsers too. It means you can start using Generators without fear of compatibility issues and show your friends the smart coding.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Benefits of Using Generators&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Lazy Evaluation:&lt;/strong&gt; Generators produce values on demand, which means they only compute values when needed.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Improved Performance:&lt;/strong&gt; By processing data in chunks, generators can help avoid blocking the main thread, leading to smoother performance and better user experience.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Simplified Code:&lt;/strong&gt; Generators can make asynchronous code easier to read and maintain by avoiding deeply nested callbacks or complex promise chains.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Memory usage:&lt;/strong&gt; It helps lower memory usage in the browser because of lazy evaluation, meaning values are generated on-the-fly rather than all at once. It can control for/while loops, fetch API execution on demand.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can run the code here: &lt;a href="https://runjs.co/s/ljJq79Wev" rel="noopener noreferrer"&gt;https://runjs.co/s/ljJq79Wev&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;API Data by {JSON} Placeholder: &lt;a href="https://jsonplaceholder.typicode.com/" rel="noopener noreferrer"&gt;https://jsonplaceholder.typicode.com/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Hope you find it helpful and added value to your knowledge.&lt;/p&gt;

&lt;p&gt;Regards.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>generators</category>
      <category>webdev</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
