<?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: Alice Kallaugher</title>
    <description>The latest articles on DEV Community by Alice Kallaugher (@kallaugher).</description>
    <link>https://dev.to/kallaugher</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%2F296792%2F20a3cd23-1d9d-44e8-b02b-9168d9ef41e8.jpg</url>
      <title>DEV Community: Alice Kallaugher</title>
      <link>https://dev.to/kallaugher</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/kallaugher"/>
    <language>en</language>
    <item>
      <title>An Introduction to JavaScript Generators</title>
      <dc:creator>Alice Kallaugher</dc:creator>
      <pubDate>Thu, 19 Dec 2019 18:18:51 +0000</pubDate>
      <link>https://dev.to/kallaugher/an-introduction-to-javascript-generators-1224</link>
      <guid>https://dev.to/kallaugher/an-introduction-to-javascript-generators-1224</guid>
      <description>&lt;p&gt;One of the fundamentals of JavaScript is that it is single-threaded, meaning that two pieces of code cannot run at the same time. If we call a function, we expect it to run to completion, blocking any other code from running. This presents challenges for any task where you need to wait for something to happen (for example, waiting for an API response). We have different tools at our disposal to help with this, including callback functions, promises, and more recently &lt;code&gt;async/await&lt;/code&gt;, introduced with ES8.&lt;/p&gt;

&lt;p&gt;A lesser known, but still very powerful tool was introduced earlier, with ES6: generators. These are similar to &lt;code&gt;async/await&lt;/code&gt; in that they let us write asynchronous code in a linear, straightforward fashion. However, they also provide the ability to &lt;strong&gt;pause and restart a function&lt;/strong&gt;, without blocking the execution of other code — exactly what we’re used to not being able to do in JavaScript!&lt;/p&gt;

&lt;p&gt;I first encountered generators through &lt;a href="https://redux-saga.js.org/" rel="noopener noreferrer"&gt;redux-saga&lt;/a&gt;, an excellent library for handling side effects in Redux. I was curious to learn about how they worked, and found them a little unintuitive at first. I spent some time digging into them, and in this post I’ll share what I found.&lt;/p&gt;

&lt;p&gt;You may recognize them from their somewhat unique syntax, with a star after the function declaration and the use of the &lt;code&gt;yield&lt;/code&gt; keyword (which can only be used within a generator function):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function* generatorFunc() {
  yield;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As their name suggests, generators &lt;em&gt;generate&lt;/em&gt; a sequence of values. Each time a generator is paused, it returns a new value, and each time it’s restarted it can take in a new argument. Following how the input and output are used can be a little tricky, so I’m going to focus on these two aspects, breaking down how generators both generate and consume data.&lt;/p&gt;

&lt;h2&gt;
  
  
  Generating data
&lt;/h2&gt;

&lt;p&gt;Generators are a type of &lt;strong&gt;iterator&lt;/strong&gt;, which are objects that define a sequence (one example is the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/@@iterator" rel="noopener noreferrer"&gt;array iterator&lt;/a&gt;. Iterators must have a &lt;code&gt;next()&lt;/code&gt; method, which is used to traverse the sequence. Each time &lt;code&gt;next()&lt;/code&gt; is called it returns an iterator response, which specifies whether the sequence is &lt;strong&gt;done&lt;/strong&gt; as well as the next &lt;strong&gt;value&lt;/strong&gt; in the sequence (or the return value if the sequence is done).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const iterator = {
  next: () =&amp;gt; ({
    value: any,
    done: boolean
  })
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Learn more about the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterator_protocol" rel="noopener noreferrer"&gt;iterator protocol&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Generators have additional behavior: they are a specific kind of iterator, returned by a &lt;strong&gt;generator function&lt;/strong&gt;. When the iterator’s &lt;code&gt;next()&lt;/code&gt; method is called, the generator function will execute until it reaches one of the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;yield&lt;/code&gt; keyword (pauses the execution)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;return&lt;/code&gt; statement (ends the execution)&lt;/li&gt;
&lt;li&gt;end of the generator function (ends the execution)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;throw&lt;/code&gt; keyword (throws an exception)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here’s an example (with &lt;code&gt;throw&lt;/code&gt; omitted for simplicity):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function* generatorFunc() {
  yield 1 + 1;
  return 2 + 2;
}

// 1.
const generatorObj = generatorFunc();

// 2.
generatorObj.next();
// returns { value: 2, done: false };

// 3.
generatorObj.next();
// returns { value: 4, done: true };
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;View code in a &lt;a href="https://jsfiddle.net/9zmuk28j/12/" rel="noopener noreferrer"&gt;jsfiddle&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Let’s break down what’s happening:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;The generator is created&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;next(&lt;/code&gt;) is called for the first time:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The generator function evaluates up to the first &lt;code&gt;yield&lt;/code&gt;, and then pauses&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;value&lt;/code&gt; is the result of the expression following &lt;code&gt;yield&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;c. &lt;code&gt;done&lt;/code&gt; is false because we haven’t reached a return statement or the end of the generator function&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;next()&lt;/code&gt; is called for a second time:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The generator function evaluation resumes&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;return&lt;/code&gt; statement is reached&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;value&lt;/code&gt; is the result of the &lt;code&gt;return&lt;/code&gt; statement&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;done&lt;/code&gt; is true, and the generator object has been consumed&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The sequence of values can also be retrieved without calling &lt;code&gt;next()&lt;/code&gt; explicitly, using array destructuring, the spread operator, or a simple &lt;code&gt;for&lt;/code&gt; loop:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function* generatorFunc() {
  yield 1 + 1;
  yield 1 + 2;

  return 2 + 2;
}

const [a, b, c] = generatorFunc();
// a = 2, b = 3, c = undefined

const values = [...generatorFunc()];
// values = [2, 3];

const vals = [];
for (const val of generatorFunc()) {
  vals.push(val);
}
// vals = [2, 3]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;View code in a &lt;a href="https://jsfiddle.net/L8r3wxy2/1/" rel="noopener noreferrer"&gt;jsfiddle&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;One important note here is that these three ways of retrieving values from a generator only take into account the &lt;code&gt;yield&lt;/code&gt; expressions, ignoring the value from the &lt;code&gt;return&lt;/code&gt; statement.&lt;/p&gt;

&lt;h2&gt;
  
  
  Consuming data
&lt;/h2&gt;

&lt;p&gt;So far we’ve looked at how generators passively generate a sequence of values; now, let’s focus on how they take in data. Most standard iterators cannot accept arguments (e.g. array iterators or set iterators), but generators can, by passing an argument to &lt;code&gt;next()&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function* generatorFunc() {
  const a = yield 1 + 1;
  const b = yield 1 + 2;

  return 2 + 2;
}
const generatorObj = generatorFunc();

// 1.
generatorObj.next(‘value 1’);
// returns { value: 2, done: false }

// 2.
generatorObj.next(‘value 2’);
// returns { value: 3, done: false }
// a = ‘value 2’

// 3.
generatorObj.next();
// returns { value: 4, done: true}
// b = undefined
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;View code in a &lt;a href="https://jsfiddle.net/v2x4k8s9/4/" rel="noopener noreferrer"&gt;jsfiddle&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Let’s break down the order of execution in a more granular way. We’ll start by focusing on the value of the variables assigned to the &lt;code&gt;yield&lt;/code&gt; expression, and the value from the iterator response returned from &lt;code&gt;next()&lt;/code&gt;:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;next()&lt;/code&gt; is called for the first time, with an argument of &lt;code&gt;'value 1'&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It reaches the first &lt;code&gt;yield&lt;/code&gt; and pauses&lt;/li&gt;
&lt;li&gt;The value returned by &lt;code&gt;next()&lt;/code&gt; is the result of the expression following the first &lt;code&gt;yield&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;next()&lt;/code&gt; is called for the second time, with an argument of &lt;code&gt;'value 2'&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The argument provides the value of the constant assigned to the first yield statement (therefore &lt;code&gt;a = 'value 2'&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;It reaches the second &lt;code&gt;yield&lt;/code&gt; and pauses&lt;/li&gt;
&lt;li&gt;The value returned by next() is the result of the expression following the second yield&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;next()&lt;/code&gt; is called for the second time, with no argument&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;There is no argument to provide the value of the constant assigned to the second yield statement (therefore &lt;code&gt;b = undefined&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;It reaches the &lt;code&gt;return&lt;/code&gt; statement and ends&lt;/li&gt;
&lt;li&gt;The value returned by &lt;code&gt;next()&lt;/code&gt; is the result of the return statement&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The most important thing to grasp here is that the argument to &lt;code&gt;next()&lt;/code&gt; provides the value for the &lt;code&gt;yield&lt;/code&gt; that had previously paused execution of the generator function. The argument passed to the first &lt;code&gt;next()&lt;/code&gt; call is ignored.&lt;/p&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&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%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Faegr8yz1c8b8spn08ab6.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%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Faegr8yz1c8b8spn08ab6.png" alt="Diagram showing the input and output of generators. All content is included in the text."&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here’s a quick summary of the main takeaways from this post.&lt;/p&gt;

&lt;p&gt;Generators:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;pause with &lt;code&gt;yield&lt;/code&gt; and restart with &lt;code&gt;next()&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;return a new value each time the function pauses or ends&lt;/li&gt;
&lt;li&gt;set each return value based on the expression following the &lt;code&gt;yield&lt;/code&gt; that paused the function&lt;/li&gt;
&lt;li&gt;take in data through arguments passed to &lt;code&gt;next()&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;set the value of the variable assigned to a &lt;code&gt;yield&lt;/code&gt; statement based on the arguments passed to the &lt;code&gt;next()&lt;/code&gt; call that restarted the function&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I hope you’ve enjoyed this quick dive into generators! If you want to dig in deeper, I recommend reading the &lt;a href="https://exploringjs.com/es6/ch_generators.html" rel="noopener noreferrer"&gt;Generators chapter of ‘Exploring ES6’&lt;/a&gt; by Axel Rauschmayer, which was very helpful in writing this article. If you want to see generators in use, &lt;a href="https://redux-saga.js.org/" rel="noopener noreferrer"&gt;redux-saga&lt;/a&gt; is definitely worth checking out as well.&lt;/p&gt;

&lt;p&gt;Let me know in the comments how you’ve used generators, or if you have any questions!&lt;/p&gt;

&lt;p&gt;&lt;em&gt;This post was originally posted on the &lt;a href="https://www.giantmachines.com/blog/an-introduction-to-javascript-generators" rel="noopener noreferrer"&gt;Giant Machines blog&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

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