<?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 David Hill on Medium]]></title>
        <description><![CDATA[Stories by David Hill on Medium]]></description>
        <link>https://medium.com/@Vidhill?source=rss-48199da0cf06------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*G2nw4D0nwIb8UFOK.jpg</url>
            <title>Stories by David Hill on Medium</title>
            <link>https://medium.com/@Vidhill?source=rss-48199da0cf06------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 30 Nov 2026 00:55:07 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@Vidhill/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[Javascript Generators and iterator.next() an edge case]]></title>
            <link>https://medium.com/@Vidhill/javascript-generators-and-iterator-next-an-edge-case-ed5ae58034d3?source=rss-48199da0cf06------2</link>
            <guid isPermaLink="false">https://medium.com/p/ed5ae58034d3</guid>
            <category><![CDATA[generator]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[es6]]></category>
            <dc:creator><![CDATA[David Hill]]></dc:creator>
            <pubDate>Mon, 09 Oct 2017 14:28:49 GMT</pubDate>
            <atom:updated>2019-02-06T15:53:31.194Z</atom:updated>
            <content:encoded><![CDATA[<p>I have been reading about Generator functions in Javascript back when they were added to the specifications in ES6/ES2015.</p><p>To be honest I couldn’t quite see the use cases for everyday coding jump out at me. <br>But, I’ve been keeping them in my back pocket hoping that my brain would see a use case when a suitable one would appear..</p><p>I ran into a situation last week, where I thought aha! 💡💡 I think a Generator could work here!</p><p>I could make use of some of the useful features of Generators i.e. pausing execution using yield and capturing results using iter.next(val)</p><p>I had a callback that would be called multiple times. I could use the generator to collect the results returned from a callback, and then use the generator’s ability to pause execution to run an action when it was called a target number of times..</p><h4>However</h4><p>My first attempts weren’t working out quite as expected..<br>I was able to get the paused execution to work, but I wasn’t seeing quite what I expecting with capturing values.</p><p>As is always a good idea, I created a reduced test case. I began to come to a conclusion, I don’t think that I can capture the value passed to the first call of iter.next()</p><p><a href="http://jsbin.com/hosolib/3/edit?js,console">Here is the JSbin</a> of my reduced test case, showing the behaviour.</p><p>I consulted the hive mind of twitter to see if my observation was correct..</p><h3>David Hill on Twitter</h3><p>@kentcdodds obscure ES6 questions.. I&#39;m using a Generator.. I want to capture the result of each call of .next() https://t.co/4dJVFOXaso</p><h3>David Hill on Twitter</h3><p>@kentcdodds It seems that the returned value of first call of .next seems inaccessible What do you think? ps I have hardly any followers, rt if you want</p><h3>Brian Terlson on Twitter</h3><p>@rossipedia @kentcdodds Confirmed. First sent value from the first .next cannot be observed since the generator starts with, essentially, an implicit yield.</p><p>So, yes, it is so.</p><p>I was also informed that there is a proposal for a solution which seems like a good idea to me.</p><h3>rossipedia on Twitter</h3><p>@kentcdodds I think that&#39;s what function.sent is meant to address: https://t.co/5RHFfj24M5</p><h4>A workaround</h4><p>One solution is, that you could just call iter.next()once after initialisation.</p><p>I created a simple factory function, that returns a function that initialises the Generator with the supplied arguments, <br>calls next()on the iterator once and then returns the iterator.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c0cea2197cb0379fbc24a4293d2f39e4/href">https://medium.com/media/c0cea2197cb0379fbc24a4293d2f39e4/href</a></iframe><p>By using this wrapper, each time I call of iter.next(val) the value is captured.</p><p>You can see a modified version of my reduced case JSbin that uses my wrapper below</p><p><a href="http://jsbin.com/masupu/edit?js,console">JS Bin</a></p><p><em>Side note: I do realise that this wrapper function wouldn’t work for every situation because, funnily enough, it makes the value returned by the first </em><em>yield inaccessible. If you do need that value, you would just have to manually call .next once.</em></p><p>Corrections, suggestions and replies welcome.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ed5ae58034d3" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>