<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Ismail Babatunde Bashiru on Medium]]></title>
        <description><![CDATA[Stories by Ismail Babatunde Bashiru on Medium]]></description>
        <link>https://medium.com/@bashy_io?source=rss-1eede9c5cac5------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*5k_fcLEXNGi9-WI0_3I6sg.jpeg</url>
            <title>Stories by Ismail Babatunde Bashiru on Medium</title>
            <link>https://medium.com/@bashy_io?source=rss-1eede9c5cac5------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 17 Nov 2026 08:20:11 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@bashy_io/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[A Simple Introduction to Generator Functions]]></title>
            <link>https://medium.com/@bashy_io/a-simple-introduction-to-generator-functions-1979814a82d0?source=rss-1eede9c5cac5------2</link>
            <guid isPermaLink="false">https://medium.com/p/1979814a82d0</guid>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[learning-to-code]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[es6]]></category>
            <dc:creator><![CDATA[Ismail Babatunde Bashiru]]></dc:creator>
            <pubDate>Wed, 24 Jun 2020 15:52:44 GMT</pubDate>
            <atom:updated>2020-07-01T13:30:54.910Z</atom:updated>
            <content:encoded><![CDATA[<h3>A Simple Introduction to Generator Functions in JavaScript</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*VGxjOL2VP5jOxv0K" /><figcaption>Photo by <a href="https://unsplash.com/@callmefred?utm_source=medium&amp;utm_medium=referral">Frederick Tubiermont</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h3><strong>GENERATOR FUNCTIONS</strong></h3><p>A generator is a function that can stop midway, then continue execution from the stopping point. Generator functions behave like an iterator. They are declared almost identically to regular functions with just one exception — the asterisks <strong>*</strong>. To declare generator functions, we need the function syntax. This means that we can’t declare them using arrow function syntax.</p><p>You can do:</p><pre>function* generator1() {<br> //code here<br>}<br>const generator2 = function *() {<br> //code here <br>}</pre><p>But, if you try to do this, you’ll get a syntax error:</p><pre>const generator3 = *() =&gt; {<br> //code here<br>}</pre><p>Which is sad because arrow functions are sexy. 😢<br><a href="https://bashiruismail.io/"><strong>Petition to allow arrow syntax for generator functions! 🌚</strong></a></p><h3>How are generator functions different from regular functions?</h3><p>Regular Functions in JavaScript run until “return” or function end. But Generator Functions run until “yield”, “return” or function end. Calling a generator function doesn’t return a value. Instead, it returns an object that can be used to iterate over the yield statements using the next() method.</p><p>What is yield? What is next()?</p><p>When you use the return keyword in a regular function, you get a single value returned on function call and the function is exited. But the yield keyword in a generator function allows us to execute a section of code to a temporary stopping point, pass back a value (in object form) before continuing execution again the next time we call the next() method on the generator function. Simply put, they allow you to “exit” your function temporarily with a value then continue execution as many times as the yield statement is used.</p><p><strong>That sounds crazy right?</strong></p><p><strong>Well, here’s an example to illustrate how it works:</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/53f7971d9e60eea4c3cf9b93cc70b166/href">https://medium.com/media/53f7971d9e60eea4c3cf9b93cc70b166/href</a></iframe><p>We can see in the example above the major difference between a generator function and a regular function.</p><p><strong>Explanation:</strong></p><ul><li>When we make the generator function call, no line in the function is executed, instead the iterator object is returned. This object is then passed to a variable so we can make a next method call on it.</li><li>The first time we call the next method, the generator executes up to the first yield statement, pauses execution and sends back an object containing the yield value and the parameter “done”. What is the use of the done parameter? It is used to signify if we’ve completed the iteration or not. It is set to false because we’re yet to complete iterating.</li><li>The next series of times we call the next method, the function resumes execution from the last yield statement and pauses again.</li><li>In our example, three are 3 yield statements but we make 4 next method calls, this is done deliberately to illustrate that although there are no more yield statements, if we didn’t make an extra next call; ‘Executing 4’ would not get printed and we would not have gotten to the end of the generator function.</li><li>The last next call returns undefined as value because there were no more yield statements. Since there are no more yield statements to temporarily halt execution, the function continues to the end and done is then set to true.</li></ul><h3>Return Statement in a Generator Function</h3><ul><li>The return statement still works in a generator function.</li><li>When used without a value, it simply exits the function.</li><li>When used with a value, that value becomes the last value in the generator iterable.</li><li>All yield statements after the return statement will be ignored in a generator function.</li></ul><p><strong>Example:</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7274debab76dae55d27fd985a239e96a/href">https://medium.com/media/7274debab76dae55d27fd985a239e96a/href</a></iframe><p>The yield statement after the return statement is ignored as the function is exited immediately. This is why the value of the second next call is undefined.</p><p><strong>Example:</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/29049611a52963d1ded653de99a8c415/href">https://medium.com/media/29049611a52963d1ded653de99a8c415/href</a></iframe><p>The second next call returns object with value 20 which is the value passed with the return statement. Like in the previous example, the yield statement after the return statement is ignored. But in this example, the value of done is automatically set to true on the return statement before the extra next() call. That is because the return statement automatically ends the iteration.</p><p>If it was a yield statement, it’d still be set to false as the generator function will not be done until “function end”. This might seem like unexpected behaviour but it is important to remember that we can still have code unexecuted after the last yield statement in a generator function. They won’t be executed until the extra next() call.</p><h3>Using the for loop to iterate over a generator function</h3><p>It is a common practice to iterate over a generator function using the for loop. This way we can automatically move to the next iteration based on the yield keyword. However, please note that using the for loop only returns the value and doesn’t let you know the status of the iteration whether done is false or true.</p><p><strong>Example:</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8798a82e484474f064ae7da487f80c35/href">https://medium.com/media/8798a82e484474f064ae7da487f80c35/href</a></iframe><p>This will print out the yielded values one by one.</p><p><strong>Please Note:</strong></p><p>While the for loop will always execute to the end of the generator function, it behaves differently when dealing with the return statement. Unlike the next() method that allows you to retrieve the value of the return statement, the for loop ignores the value it returns altogether.</p><p><strong>Example:</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d303124f03e34acec9820d6d82275894/href">https://medium.com/media/d303124f03e34acec9820d6d82275894/href</a></iframe><pre><strong>Output:</strong></pre><pre>Start<br>8<br>9<br>10<br>11<br>After all yields</pre><p>As we can see in the example above, the for loop continues execution after the last yield statement to the end of the function, but doesn’t receive the value 20 returned. The last value it receives is 11 which is passed by a yield statement.</p><h3>Demonstrating the usage of Variables in a Generator Function</h3><p>One of the things that make a generator function so special is how they maintain variable value on pause. Their context (variable bindings) is saved across re-entrances.</p><p><strong>Example:</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1780840d129ce52f29e54072c8e84baf/href">https://medium.com/media/1780840d129ce52f29e54072c8e84baf/href</a></iframe><p>As illustrated in the example above, when the generator function pauses execution after a yield statement, the value of the variables will not change and will be maintained until the function resumes.</p><h3>Talking Back to Generators</h3><p>Communication with generators can happen in both directions. Asides receiving values from a generator, we can also inject values back into the function using the next() method. This values can be received using the yield statement.</p><p><strong>Example:</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/41c26bc72cf4c0181d37e8730e8708a9/href">https://medium.com/media/41c26bc72cf4c0181d37e8730e8708a9/href</a></iframe><p>In the example above, the function will continue yielding ‘Wrong Answer! Try again’ until the right name is guessed. We pass the value to the generator using the next() method.</p><h3>Conclusion</h3><p>I hope I have been able to successfully introduce you to generator functions.</p><p><em>Please follow me on my GitHub account for more JavaScript and Full-Stack projects: </em><a href="https://github.com/bashyio"><em>https://github.com/bashyio</em></a><em>. Or check out my Personal Website for my Portfolio: </em><a href="https://bashiruismail.io/"><em>https://bashiruismail.io</em></a><em>/</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1979814a82d0" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>