<?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: Arihant1987</title>
    <description>The latest articles on DEV Community by Arihant1987 (@arihantbanthia).</description>
    <link>https://dev.to/arihantbanthia</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%2F633458%2Fc1be0cbe-1231-4f65-9010-75f34cde7804.png</url>
      <title>DEV Community: Arihant1987</title>
      <link>https://dev.to/arihantbanthia</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/arihantbanthia"/>
    <language>en</language>
    <item>
      <title>Generator Functions in Javascript</title>
      <dc:creator>Arihant1987</dc:creator>
      <pubDate>Mon, 24 May 2021 06:45:43 +0000</pubDate>
      <link>https://dev.to/arihantbanthia/generator-functions-in-javascript-4b35</link>
      <guid>https://dev.to/arihantbanthia/generator-functions-in-javascript-4b35</guid>
      <description>&lt;p&gt;Before understanding generators, we need to take a look on iterables.&lt;/p&gt;

&lt;h2&gt;
  
  
  Iterables
&lt;/h2&gt;

&lt;p&gt;Iterables are objects which can be iterated. Like array,set etc. So if you log an array and check it's prototype, you will find one property &lt;code&gt;Symbol.iterator&lt;/code&gt; which is a function that can be used to make an array iterable. &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%2Fuploads%2Farticles%2Fvnn4kx9crisoapc4rae8.png" 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%2Fuploads%2Farticles%2Fvnn4kx9crisoapc4rae8.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So after execution of following code you can check an iterator and how to get values through it :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
let numArray = [1,2,3,4];
console.dir(numArray);
const numIterable = numArray[Symbol.iterator]();
console.log(numIterable.next());
console.log(numIterable.next());
console.log(numIterable.next());
console.log(numIterable.next());
console.log(numIterable.next());

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

&lt;/div&gt;



&lt;p&gt;and you will see following output : &lt;br&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%2Fuploads%2Farticles%2Fr09a32xj9klbivcbg06v.png" 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%2Fuploads%2Farticles%2Fr09a32xj9klbivcbg06v.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;next&lt;/code&gt; function return one object, which has two keys : &lt;code&gt;value&lt;/code&gt; and &lt;code&gt;done&lt;/code&gt;. After last item in iterable, value will be undefined and done becomes true.&lt;/p&gt;

&lt;h2&gt;
  
  
  Generators :
&lt;/h2&gt;

&lt;p&gt;Generator are basically functions which can return more than one values, represented by &lt;code&gt;function* &amp;lt;name&amp;gt;&lt;/code&gt; or &lt;code&gt;function *&amp;lt;name&amp;gt;&lt;/code&gt;. By calling generator, it doesn't return values instead returns a generator object. To get values, you have to call it's &lt;code&gt;next()&lt;/code&gt; method, which will return an object having structure&lt;br&gt;
&lt;code&gt;{value: &amp;lt;value&amp;gt;, done: true/false}&lt;/code&gt;. Please check below example which returns multiple of 5.&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%2Fuploads%2Farticles%2Fh0ar9qsj2geus1qo2br6.png" 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%2Fuploads%2Farticles%2Fh0ar9qsj2geus1qo2br6.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In above example, you can see that by calling &lt;code&gt;generateMultipleFive&lt;/code&gt; function, it is returning an object, then we can call next() method on that object. It will return an object having value and done as keys. &lt;br&gt;
So the question comes, it is only first value, how we can get all values ? Answer is, you have to run &lt;code&gt;next&lt;/code&gt; method every time to get next values. &lt;code&gt;done&lt;/code&gt; property will return false in every case.&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%2Fuploads%2Farticles%2F5v8x73zlchoxa1pk59yl.png" 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%2Fuploads%2Farticles%2F5v8x73zlchoxa1pk59yl.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Also, after returning last value, if you will run next method again, it will return done as true. &lt;strong&gt;If we use &lt;code&gt;return&lt;/code&gt; instead of &lt;code&gt;yield&lt;/code&gt; for returning last value, then &lt;code&gt;done&lt;/code&gt; will be &lt;code&gt;true&lt;/code&gt; for that case&lt;/strong&gt; . Please run below code in console to check this behavior : &lt;br&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%2Fuploads%2Farticles%2Fm3fwjypamzkjyvg4go5n.png" 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%2Fuploads%2Farticles%2Fm3fwjypamzkjyvg4go5n.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Relation between Generators and iterable
&lt;/h3&gt;

&lt;p&gt;Since genrators have next method and calling next gives object containing value and done as keys, so basically generators are iterable. You can use for...of loop to get all values. See below example &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%2Fuploads%2Farticles%2Fwcfagd9xmq79r57rgn20.png" 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%2Fuploads%2Farticles%2Fwcfagd9xmq79r57rgn20.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Nesting In Generators
&lt;/h3&gt;

&lt;p&gt;A generator can be used inside another generator called Generator Composition. Special syntax &lt;code&gt;yield* &amp;lt;function name&amp;gt;&lt;/code&gt; is used for achieving this. In below example, I have used composition of generators to print tables of three numbers. Outer generator calls inner generator through &lt;code&gt;yield*&lt;/code&gt; syntax, which yields numbers in table.&lt;br&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%2Fuploads%2Farticles%2Ffr79zu2bw3y2389xsot1.png" 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%2Fuploads%2Farticles%2Ffr79zu2bw3y2389xsot1.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  How generators are different from iterables
&lt;/h3&gt;

&lt;p&gt;Till now, you can say that iterables also can do stuff which generator can do, then how generator is different ? Answer is, &lt;strong&gt;Combination of yield and next() can be used to not only returning the result, but also passing the value inside the generator&lt;/strong&gt;.To do so, we should call generator.next(arg), with an argument. That argument becomes the result of yield. Check below example for this : &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%2Fuploads%2Farticles%2F71vlximbcve06zafwhps.png" 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%2Fuploads%2Farticles%2F71vlximbcve06zafwhps.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Execution stops at line &lt;code&gt;yield "2 + 2 = ?"&lt;/code&gt; and when statement &lt;code&gt;generator.next().value&lt;/code&gt; is called, it assign question string to variable question. And statement &lt;code&gt;generator.next(4);&lt;/code&gt; assign result to result variable, where execution was paused in generator function.&lt;br&gt;
So this was a basic understanding of Generators. &lt;/p&gt;

</description>
      <category>generators</category>
      <category>iterables</category>
      <category>es6</category>
      <category>composition</category>
    </item>
  </channel>
</rss>
