A place to cache linked articles (think custom and personal wayback machine)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

4 年之前
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717
  1. title: ES6 generators in depth
  2. url: http://www.2ality.com/2015/03/es6-generators.html
  3. hash_url: e826765f4c8c47da6b78aa2ad1343cc6
  4. <p>This blog post is part of a series on iteration in ES6:</p>
  5. <ol>
  6. <li><a href="http://www.2ality.com/2015/02/es6-iteration.html">Iterables and iterators in ECMAScript 6</a></li>
  7. <li><a href="http://www.2ality.com/2015/03/es6-generators.html">ES6 generators in depth</a></li>
  8. </ol>
  9. <hr/>
  10. <p>Generators, a new feature of ECMAScript 6 <a class="ptr">[4]</a>, are functions that can be paused and resumed. This helps with many applications: iterators, asynchronous programming, etc. This blog post explains how generators work and gives an overview of their applications.</p>
  11. <p>The following GitHub repository contains the example code: <a href="https://github.com/rauschma/generator-examples">generator-examples</a></p>
  12. <h3 id="overview" class="countheads">Overview</h3><p>Two important applications of generators are:</p>
  13. <ul>
  14. <li>Implementing iterables</li>
  15. <li>Blocking on asynchronous function calls</li>
  16. </ul>
  17. <p>The following subsections give brief overviews of these applications, more thorough explanations are provided later (plus discussions of other topics).</p>
  18. <h4 id="implementing_iterables_via_generators">Implementing iterables via generators</h4><p>The following function returns an iterable over the properties of an object, one <code>[key,value]</code> pair per property:</p>
  19. <pre class="javascript">
  20. // The asterisk after `function` means that
  21. // `objectEntries` is a generator
  22. function* objectEntries(obj) {
  23. let propKeys = Reflect.ownKeys(obj);
  24. for (let propKey of propKeys) {
  25. // `yield` returns a value and then pauses
  26. // the generator. Later, execution continues
  27. // where it was previously paused.
  28. yield [propKey, obj[propKey]];
  29. }
  30. }
  31. </pre>
  32. <p>How exactly <code>objectEntries()</code> works is explained later. It is used like this:</p>
  33. <pre class="javascript">
  34. let jane = { first: 'Jane', last: 'Doe' };
  35. for (let [key,value] of objectEntries(jane)) {
  36. console.log(`${key}: ${value}`);
  37. }
  38. // Output:
  39. // first: Jane
  40. // last: Doe
  41. </pre>
  42. <h4 id="blocking_on_asynchronous_function_calls">Blocking on asynchronous function calls</h4><p>In the following code, I use <a href="https://github.com/tj/co">the control flow library co</a> to asynchronously retrieve two JSON files. Note how, in line (A), execution blocks (waits) until the result of <code>Promise.all()</code> is ready. That means that the code looks synchronous while performing asynchronous operations.</p>
  43. <pre class="javascript">
  44. co(function* () {
  45. try {
  46. let [croftStr, bondStr] = yield Promise.all([ // (A)
  47. getFile('http://localhost:8000/croft.json'),
  48. getFile('http://localhost:8000/bond.json'),
  49. ]);
  50. let croftJson = JSON.parse(croftStr);
  51. let bondJson = JSON.parse(bondStr);
  52. console.log(croftJson);
  53. console.log(bondJson);
  54. } catch (e) {
  55. console.log('Failure to read: ' + e);
  56. }
  57. });
  58. </pre>
  59. <p><code>getFile(url)</code> retrieves the file pointed to by <code>url</code>. Its implementation is shown later. I’ll also explain how <code>co</code> works.</p>
  60. <h3 id="what_are_generators%3F">What are generators?</h3><p><em>Generators</em> are functions that can be paused and resumed, which enables a variety of applications.</p>
  61. <p>As a first example, consider the following generator function whose name is <code>genFunc</code>:</p>
  62. <pre class="javascript">
  63. function* genFunc() {
  64. console.log('First');
  65. yield; // (A)
  66. console.log('Second'); // (B)
  67. }
  68. </pre>
  69. <p>Two things distinguish <code>genFunc</code> from a normal function declaration:</p>
  70. <ul>
  71. <li>It starts with the “keyword” <code>function*</code>.</li>
  72. <li>It is paused in the middle via <code>yield</code>.</li>
  73. </ul>
  74. <p>Calling <code>genFunc</code> does not execute it. Instead, it returns a so-called <em>generator object</em> that lets us control <code>genFunc</code>’s execution:</p>
  75. <pre class="repl">
  76. &gt; let genObj = genFunc();
  77. </pre>
  78. <p><code>genFunc()</code> is initially suspended at the beginning of its body. The method <code>genObj.next()</code> continues the execution of <code>genFunc</code>, until the next <code>yield</code>:</p>
  79. <pre class="repl">
  80. &gt; genObj.next()
  81. First
  82. { value: undefined, done: false }
  83. </pre>
  84. <p>As you can see in the last line, <code>genObj.next()</code> also returns an object. Let’s ignore that for now. It will matter once we look at generators as iterators.</p>
  85. <p><code>genFunc</code> is now paused in line (A). If we call <code>next()</code> again, execution resumes and line (B) is executed:</p>
  86. <pre class="repl">
  87. &gt; genObj.next()
  88. Second
  89. { value: undefined, done: true }
  90. </pre>
  91. <p>Afterwards, the function is finished, execution has left the body and further calls of <code>genObj.next()</code> have no effect.</p>
  92. <h4 id="ways_of_creating_generators">Ways of creating generators</h4><p>There are four ways in which you can create generators:</p>
  93. <ol>
  94. <li><p>Via a generator function declaration:</p>
  95. <pre class="javascript">
  96. function* genFunc() { ··· }
  97. let genObj = genFunc();
  98. </pre>
  99. </li>
  100. <li><p>Via a generator function expression:</p>
  101. <pre class="javascript">
  102. const genFunc = function* () { ··· };
  103. let genObj = genFunc();
  104. </pre>
  105. </li>
  106. <li><p>Via a generator method definition in an object literal:</p>
  107. <pre class="javascript">
  108. let obj = {
  109. * generatorMethod() {
  110. ···
  111. }
  112. };
  113. let genObj = obj.generatorMethod();
  114. </pre>
  115. </li>
  116. <li><p>Via a generator method definition in a class definition (which can be a class declaration or a class expression <a class="ptr">[11]</a>):</p>
  117. <pre class="javascript">
  118. class MyClass {
  119. * generatorMethod() {
  120. ···
  121. }
  122. }
  123. let myInst = new MyClass();
  124. let genObj = myInst.generatorMethod();
  125. </pre>
  126. </li>
  127. </ol>
  128. <h4 id="roles_played_by_generators">Roles played by generators</h4><p>Generators can play three roles:</p>
  129. <ol>
  130. <li><p>Iterators (data producers): Each <code>yield</code> can return a value via <code>next()</code>, which means that generators can produce sequences of values via loops and recursion. Due to generator objects implementing the interface <code>Iterable</code> <a class="ptr">[5]</a>, these sequences can be processed by any ECMAScript 6 construct that supports iterables. Two examples are: <code>for-of</code> loops and the spread operator (<code>...</code>).</p>
  131. </li>
  132. <li><p>Observers (data consumers): <code>yield</code> can also receive a value from <code>next()</code> (via a parameter). That means that generators become data consumers that pause until a new value is pushed into them via <code>next()</code>.</p>
  133. </li>
  134. <li><p>Coroutines (data producers and consumers): Given that generators are pausable and can be both data producers and data consumers, not much work is needed to turn them into coroutines (cooperatively multitasked tasks).</p>
  135. </li>
  136. </ol>
  137. <p>The next sections provide deeper explanations of these roles.</p>
  138. <h3 id="generators_as_iterators_%28data_production%29">Generators as iterators (data production)</h3><p>As explained before, generator objects can be data producers, data consumers or both. This section looks at them as data producers, where they implement both the interfaces <code>Iterable</code> and <code>Iterator</code> (shown below). Note that that means that the result of a generator function is both an iterable and an iterator. The full interface of generator objects will be shown later.</p>
  139. <pre class="javascript">
  140. interface Iterable {
  141. [Symbol.iterator]() : Iterator;
  142. }
  143. interface Iterator {
  144. next() : IteratorResult;
  145. return?(value? : any) : IteratorResult;
  146. }
  147. interface IteratorResult {
  148. value : any;
  149. done : boolean;
  150. }
  151. </pre>
  152. <p>A generator function produces a sequence of values via <code>yield</code>, a data consumer consumes thoses values via the iterator method <code>next()</code>. For example, the following generator function produces the values <code>'a'</code> and <code>'b'</code>:</p>
  153. <pre class="javascript">
  154. function* genFunc() {
  155. yield 'a';
  156. yield 'b';
  157. }
  158. </pre>
  159. <p>This interaction shows how to retrieve the yielded values via the generator object <code>genObj</code>:</p>
  160. <pre class="repl">
  161. &gt; let genObj = genFunc();
  162. &gt; genObj.next()
  163. { value: 'a', done: false }
  164. &gt; genObj.next()
  165. { value: 'b', done: false }
  166. &gt; genObj.next() // end of sequence reached
  167. ( value: undefined, done: true }
  168. </pre>
  169. <h4 id="ways_of_iterating_over_a_generator">Ways of iterating over a generator</h4><p>As generator objects are iterable, ES6 language constructs that support iterables can be applied to them. The following three ones are especially important.</p>
  170. <p>First, the <code>for-of</code> loop:</p>
  171. <pre class="javascript">
  172. for (let x of genFunc()) {
  173. console.log(x);
  174. }
  175. // Output:
  176. // a
  177. // b
  178. </pre>
  179. <p>Second, the spread operator (<code>...</code>), which turns iterated sequences into elements of an array (consult <a class="ptr">[7]</a> for more information on this operator):</p>
  180. <pre class="javascript">
  181. let arr = [...genFunc()]; // ['a', 'b']
  182. </pre>
  183. <p>Third, destructuring <a class="ptr">[6]</a>:</p>
  184. <pre class="repl">
  185. &gt; let [x, y] = genFunc();
  186. &gt; x
  187. 'a'
  188. &gt; y
  189. 'b'
  190. </pre>
  191. <h4 id="returning_from_a_generator">Returning from a generator</h4><p>The previous generator function did not contain an explicit <code>return</code>. An implicit <code>return</code> is equivalent to returning <code>undefined</code>. Let’s examine a generator with an explicit <code>return</code>:</p>
  192. <pre class="javascript">
  193. function* genFuncWithReturn() {
  194. yield 'a';
  195. yield 'b';
  196. return 'result';
  197. }
  198. </pre>
  199. <p>The returned value shows up in the last object returned by <code>next()</code>, whose property <code>done</code> is <code>true</code>:</p>
  200. <pre class="repl">
  201. &gt; let genObjWithReturn = genFuncWithReturn();
  202. &gt; genObjWithReturn.next()
  203. { value: 'a', done: false }
  204. &gt; genObjWithReturn.next()
  205. { value: 'b', done: false }
  206. &gt; genObjWithReturn.next()
  207. { value: 'result', done: true }
  208. </pre>
  209. <p>However, most constructs that work with iterables ignore the value inside the <code>done</code> object:</p>
  210. <pre class="javascript">
  211. for (let x of genFuncWithReturn()) {
  212. console.log(x);
  213. }
  214. // Output:
  215. // a
  216. // b
  217. let arr = [...genFuncWithReturn()]; // ['a', 'b']
  218. </pre>
  219. <p><code>yield*</code>, an operator for making recursive generator calls, does consider values inside <code>done</code> objects. It is explained later.</p>
  220. <h4 id="example%3A_iterating_over_properties">Example: iterating over properties</h4><p>Let’s look at an example that demonstrates how convenient generators are for implementing iterables. The following function, <code>objectEntries()</code>, returns an iterable over the properties of an object:</p>
  221. <pre class="javascript">
  222. function* objectEntries(obj) {
  223. // In ES6, you can use strings or symbols as property keys,
  224. // Reflect.ownKeys() retrieves both
  225. let propKeys = Reflect.ownKeys(obj);
  226. for (let propKey of propKeys) {
  227. yield [propKey, obj[propKey]];
  228. }
  229. }
  230. </pre>
  231. <p>This function enables you to iterate over the properties of an object <code>jane</code> via the <code>for-of</code> loop:</p>
  232. <pre class="javascript">
  233. let jane = { first: 'Jane', last: 'Doe' };
  234. for (let [key,value] of objectEntries(jane)) {
  235. console.log(`${key}: ${value}`);
  236. }
  237. // Output:
  238. // first: Jane
  239. // last: Doe
  240. </pre>
  241. <p>For comparison – an implementation of <code>objectEntries()</code> that doesn’t use generators is much more complicated:</p>
  242. <pre class="javascript">
  243. function objectEntries(obj) {
  244. let index = 0;
  245. let propKeys = Reflect.ownKeys(obj);
  246. return {
  247. [Symbol.iterator]() {
  248. return this;
  249. },
  250. next() {
  251. if (index &lt; propKeys.length) {
  252. let key = propKeys[index];
  253. index++;
  254. return { value: [key, obj[key]] };
  255. } else {
  256. return { done: true };
  257. }
  258. }
  259. };
  260. }
  261. </pre>
  262. <h4 id="recursion_via_yield*_%28for_output%29">Recursion via <code>yield*</code> (for output)</h4>
  263. <p>The <code>yield*</code> operator lets you call another generator from within a generator, as if you made a function call. For now, I only explain how this works if both generators produce output, I’ll later explain how things work if input is involved.</p>
  264. <p>How can one generator recursively call another generator? Let’s assume you have written a generator function <code>foo</code>:</p>
  265. <pre class="javascript">
  266. function* foo() {
  267. yield 'a';
  268. yield 'b';
  269. }
  270. </pre>
  271. <p>How would you call <code>foo</code> from another generator function <code>bar</code>? The following approach does not work!</p>
  272. <pre class="javascript">
  273. function* bar() {
  274. yield 'x';
  275. foo(); // does nothing!
  276. yield 'y';
  277. }
  278. </pre>
  279. <p>Calling <code>foo()</code> returns an object, but does not actually execute <code>foo()</code>. That’s why ECMAScript 6 has the operator <code>yield*</code> for making recursive generator calls:</p>
  280. <pre class="javascript">
  281. function* bar() {
  282. yield 'x';
  283. yield* foo();
  284. yield 'y';
  285. }
  286. // Collect all values yielded by bar() in an array
  287. let arr = [...bar()];
  288. // ['x', 'a', 'b', 'y']
  289. </pre>
  290. <p>Internally, <code>yield*</code> works roughly as follows:</p>
  291. <pre class="javascript">
  292. function* bar() {
  293. yield 'x';
  294. for (let value of foo()) {
  295. yield value;
  296. }
  297. yield 'y';
  298. }
  299. </pre>
  300. <p>The operand of <code>yield*</code> does not have to be a generator object, it can be any iterable:</p>
  301. <pre class="javascript">
  302. function* bla() {
  303. yield 'sequence';
  304. yield* ['of', 'yielded'];
  305. yield 'values';
  306. }
  307. let arr = [...bla()];
  308. // ['sequence', 'of', 'yielded', 'values']
  309. </pre>
  310. <h5 id="yield*_considers_end-of-iteration_values"><code>yield*</code> considers end-of-iteration values</h5><p>Most constructs that support iterables ignore the value included in the end-of-iteration object (whose property <code>done</code> is <code>true</code>). Generators provide that value via <code>return</code>. The result of <code>yield*</code> is the end-of-iteration value:</p>
  311. <pre class="javascript">
  312. function* genFuncWithReturn() {
  313. yield 'a';
  314. yield 'b';
  315. return 'The result';
  316. }
  317. function* logReturned(genObj) {
  318. let result = yield* genObj;
  319. console.log(result); // (A)
  320. }
  321. </pre>
  322. <p>If we want to get to line (A), we first must iterate over all values yielded by <code>logReturned()</code>:</p>
  323. <pre class="repl">
  324. &gt; [...logReturned(genFuncWithReturn())]
  325. The result
  326. [ 'a', 'b' ]
  327. </pre>
  328. <h5 id="iterating_over_trees">Iterating over trees</h5><p>Iterating over a tree with recursion is simple, writing an iterator for a tree with traditional means is complicated. That’s why generators shine here: they let you implement an iterator via recursion. As an example, consider the following data structure for binary trees. It is iterable, because it has a method whose key is <code>Symbol.iterator</code>. That method is a generator method and returns an iterator when called.</p>
  329. <pre class="javascript">
  330. class BinaryTree {
  331. constructor(value, left=null, right=null) {
  332. this.value = value;
  333. this.left = left;
  334. this.right = right;
  335. }
  336. /** Prefix iteration */
  337. * [Symbol.iterator]() {
  338. yield this.value;
  339. if (this.left) {
  340. yield* this.left;
  341. }
  342. if (this.right) {
  343. yield* this.right;
  344. }
  345. }
  346. }
  347. </pre>
  348. <p>The following code creates a binary tree and iterates over it via <code>for-of</code>:</p>
  349. <pre class="javascript">
  350. let tree = new BinaryTree('a',
  351. new BinaryTree('b',
  352. new BinaryTree('c'),
  353. new BinaryTree('d')),
  354. new BinaryTree('e'));
  355. for (let x of tree) {
  356. console.log(x);
  357. }
  358. // Output:
  359. // a
  360. // b
  361. // c
  362. // d
  363. // e
  364. </pre>
  365. <h4 id="you_can_only_yield_in_generators">You can only <code>yield</code> in generators</h4><p>A significant limitation of generators is that you can only yield while you are (statically) inside a generator function. That is, yielding in callbacks doesn’t work:</p>
  366. <pre class="javascript">
  367. function* genFunc() {
  368. ['a', 'b'].forEach(x =&gt; yield x); // SyntaxError
  369. }
  370. </pre>
  371. <p><code>yield</code> is not allowed inside non-generator functions, which is why the previous code causes a syntax error. In this case, it is easy to rewrite the code so that it doesn’t use callbacks (as shown below). But unfortunately that isn’t always possibe.</p>
  372. <pre class="javascript">
  373. function* genFunc() {
  374. for (let x of ['a', 'b']) {
  375. yield x; // OK
  376. }
  377. }
  378. </pre>
  379. <p>The upsides of this limitation are explained later: they make generators easier to implement and compatible with event loops.</p>
  380. <h3 id="generators_as_observers_%28data_consumption%29">Generators as observers (data consumption)</h3><p>As consumers of data, generator objects conform to the second half of the generator interface, <code>Observer</code>:</p>
  381. <pre class="javascript">
  382. interface Observer {
  383. next(value? : any) : void;
  384. return(value? : any) : void;
  385. throw(error) : void;
  386. }
  387. </pre>
  388. <p>As an observer, a generator pauses until it receives input. There are three kinds of input, transmitted via the methods specified by the interface:</p>
  389. <ul>
  390. <li><code>next()</code> sends normal input.</li>
  391. <li><code>return()</code> terminates the generator.</li>
  392. <li><code>throw()</code> signals an error.</li>
  393. </ul>
  394. <h4 id="sending_values_via_next%28%29">Sending values via <code>next()</code></h4><p>If you use a generator as an observer, you send values to it via <code>next()</code> and it receives those values via <code>yield</code>:</p>
  395. <pre class="javascript">
  396. function* dataConsumer() {
  397. console.log('Started');
  398. console.log(`1. ${yield}`); // (A)
  399. console.log(`2. ${yield}`);
  400. return 'result';
  401. }
  402. </pre>
  403. <p>Let’s use this generator interactively. First, we create a generator object:</p>
  404. <pre class="repl">
  405. &gt; let genObj = dataConsumer();
  406. </pre>
  407. <p>We now call <code>genObj.next()</code>, which starts the generator. Execution continues until the first <code>yield</code>, which is where the generator pauses. The result of <code>next()</code> is the value yielded in line (A) (<code>undefined</code>, because <code>yield</code> doesn’t have an operand). In this section, we are not interested in what <code>next()</code> returns, because we only use it to send values, not to retrieve values.</p>
  408. <pre class="javascript">
  409. &gt; genObj.next()
  410. Started
  411. { value: undefined, done: false }
  412. </pre>
  413. <p>We call <code>next()</code> two more times, in order to send the value <code>'a'</code> to the first <code>yield</code> and the value <code>'b'</code> to the second <code>yield</code>:</p>
  414. <pre class="javascript">
  415. &gt; genObj.next('a')
  416. 1. a
  417. { value: undefined, done: false }
  418. &gt; genObj.next('b')
  419. 2. b
  420. { value: 'result', done: true }
  421. </pre>
  422. <p>The result of the last <code>next()</code> is the value returned from <code>dataConsumer()</code>. <code>done</code> being <code>true</code> indicates that the generator is finished.</p>
  423. <p>Unfortunately, <code>next()</code> is asymmetric, but that can’t be helped: It always sends a value to the currently suspended <code>yield</code>, but returns the operand of the following <code>yield</code>.</p>
  424. <h5 id="the_first_next%28%29">The first <code>next()</code></h5><p>When using a generator as an observer, it is important to note that the only purpose of the first invocation of <code>next()</code> is to start the observer. It is only ready for input afterwards, because this first invocation has advanced execution to the first <code>yield</code>. Therefore, you can’t send input via the first <code>next()</code> – you even get an error if you do:</p>
  425. <pre class="repl">
  426. &gt; function* g() { yield }
  427. &gt; g().next('hello')
  428. TypeError: attempt to send 'hello' to newborn generator
  429. </pre>
  430. <p>The following utility function fixes this issue:</p>
  431. <pre class="javascript">
  432. /**
  433. * Returns a function that, when called,
  434. * returns a generator object that is immediately
  435. * ready for input via `next()`
  436. */
  437. function coroutine(generatorFunction) {
  438. return function (...args) {
  439. let generatorObject = generatorFunction(...args);
  440. generatorObject.next();
  441. return generatorObject;
  442. };
  443. }
  444. </pre>
  445. <p>To see how <code>coroutine()</code> works, let’s compare a wrapped generator with a normal one:</p>
  446. <pre class="javascript">
  447. const wrapped = coroutine(function* () {
  448. console.log(`First input: ${yield}`);
  449. return 'DONE';
  450. });
  451. const normal = function* () {
  452. console.log(`First input: ${yield}`);
  453. return 'DONE';
  454. };
  455. </pre>
  456. <p>The wrapped generator is immediately ready for input:</p>
  457. <pre class="repl">
  458. &gt; wrapped().next('hello!')
  459. First input: hello!
  460. </pre>
  461. <p>The normal generator needs an extra <code>next()</code> until it is ready for input:</p>
  462. <pre class="repl">
  463. &gt; let genObj = normal();
  464. &gt; genObj.next()
  465. { value: undefined, done: false }
  466. &gt; genObj.next('hello!')
  467. First input: hello!
  468. { value: 'DONE', done: true }
  469. </pre>
  470. <h4 id="yield_binds_loosely"><code>yield</code> binds loosely</h4><p><code>yield</code> binds very loosely, so that we don’t have to put its operand in parentheses:</p>
  471. <pre class="javascript">
  472. yield a + b + c;
  473. </pre>
  474. <p>This is treated as:</p>
  475. <pre class="javascript">
  476. yield (a + b + c);
  477. </pre>
  478. <p>Not as:</p>
  479. <pre class="javascript">
  480. (yield a) + b + c;
  481. </pre>
  482. <p>As a consequence, many operators bind more tightly than <code>yield</code> and you have to put <code>yield</code> in parentheses if you want to use it as an operand. For example, you get a SyntaxError if you make an unparenthesized <code>yield</code> an operand of plus:</p>
  483. <pre class="javascript">
  484. console.log('Hello' + yield); // SyntaxError
  485. console.log('Hello' + yield 123); // SyntaxError
  486. console.log('Hello' + (yield)); // OK
  487. console.log('Hello' + (yield 123)); // OK
  488. </pre>
  489. <p>You do not need parens if <code>yield</code> is a direct argument in a function or method call:</p>
  490. <pre class="javascript">
  491. foo(yield 'a', yield 'b');
  492. </pre>
  493. <p>You also don’t need parens if you use <code>yield</code> on the right-hand side of an assignment:</p>
  494. <pre class="javascript">
  495. let input = yield;
  496. </pre>
  497. <h5 id="grammar">Grammar</h5><p>The need for parens can be seen in the following grammar rules in the <a href="https://people.mozilla.org/~jorendorff/es6-draft.html#sec-expressions">ECMAScript 6 specification</a>. These rules describe how expressions are parsed. I list them here from general (loose binding, lower precedence) to specific (tight binding, higher precedence). Wherever a certain kind of expression is demanded, you can also use more specific ones. The opposite is not true. The hierarchy ends with <code>ParenthesizedExpression</code>, which means that you can mention any expression anywhere, if you put it in parentheses.</p>
  498. <pre class="undefined">
  499. Expression :
  500. AssignmentExpression
  501. Expression , AssignmentExpression
  502. AssignmentExpression :
  503. ConditionalExpression
  504. YieldExpression
  505. ArrowFunction
  506. LeftHandSideExpression = AssignmentExpression
  507. LeftHandSideExpression AssignmentOperator AssignmentExpression
  508. ···
  509. AdditiveExpression :
  510. MultiplicativeExpression
  511. AdditiveExpression + MultiplicativeExpression
  512. AdditiveExpression - MultiplicativeExpression
  513. MultiplicativeExpression :
  514. UnaryExpression
  515. MultiplicativeExpression MultiplicativeOperator UnaryExpression
  516. ···
  517. PrimaryExpression :
  518. this
  519. IdentifierReference
  520. Literal
  521. ArrayLiteral
  522. ObjectLiteral
  523. FunctionExpression
  524. ClassExpression
  525. GeneratorExpression
  526. RegularExpressionLiteral
  527. TemplateLiteral
  528. ParenthesizedExpression
  529. ParenthesizedExpression :
  530. ( Expression )
  531. </pre>
  532. <p>The operands of an <code>AdditiveExpression</code> are an <code>AdditiveExpression</code> and a <code>MultiplicativeExpression</code>. Therefore, using a (more specific) <code>ParenthesizedExpression</code> as an operand is OK, but using a (more general) <code>YieldExpression</code> isn’t.</p>
  533. <h4 id="return%28%29_and_throw%28%29"><code>return()</code> and <code>throw()</code></h4><p>Let’s recap how <code>next(x)</code> works (after the first invocation):</p>
  534. <ol>
  535. <li>The generator is currently suspended at a <code>yield</code> operator.</li>
  536. <li>Send the value <code>x</code> to that <code>yield</code>, which means that it evaluates to <code>x</code>.</li>
  537. <li>Proceed to the next <code>yield</code> or <code>return</code>:<ul>
  538. <li><code>yield x</code> leads to <code>next()</code> returning with <code>{ value: x, done: false }</code></li>
  539. <li><code>return x</code> leads to <code>next()</code> returning with <code>{ value: x, done: true }</code></li>
  540. </ul>
  541. </li>
  542. </ol>
  543. <p><code>return()</code> and <code>throw()</code> work similarly to <code>next()</code>, but they do something different in step 2:</p>
  544. <ul>
  545. <li><code>return(x)</code> executes <code>return x</code> at the location of <code>yield</code>.</li>
  546. <li><code>throw(x)</code> executes <code>throw x</code> at the location of <code>yield</code>.</li>
  547. </ul>
  548. <h4 id="return%28%29_terminates_the_generator"><code>return()</code> terminates the generator</h4><p><code>return()</code> performs a <code>return</code> at the location of the <code>yield</code> that led to the last suspension of the generator. Let’s use the following generator function to see how that works.</p>
  549. <pre class="javascript">
  550. function* genFunc1() {
  551. try {
  552. console.log('Started');
  553. yield; // (A)
  554. } finally {
  555. console.log('Exiting');
  556. }
  557. }
  558. </pre>
  559. <p>In the following interaction, we first use <code>next()</code> to start the generator and to proceed until the <code>yield</code> in line (A). Then we return from that location via <code>return()</code>.</p>
  560. <pre class="repl">
  561. &gt; let genObj1 = genFunc1();
  562. &gt; genObj1.next()
  563. Started
  564. { value: undefined, done: false }
  565. &gt; genObj1.return('Result')
  566. Exiting
  567. { value: 'Result', done: true }
  568. </pre>
  569. <h5 id="preventing_termination">Preventing termination</h5><p>You can prevent <code>return()</code> from terminating the generator if you yield inside the <code>finally</code> clause (using a <code>return</code> statement in that clause is also possible):</p>
  570. <pre class="javascript">
  571. function* genFunc2() {
  572. try {
  573. console.log('Started');
  574. yield;
  575. } finally {
  576. yield 'Not done, yet!';
  577. }
  578. }
  579. </pre>
  580. <p>This time, <code>return()</code> does not exit the generator function. Accordingly, the property <code>done</code> of the object it returns is <code>false</code>.</p>
  581. <pre class="repl">
  582. &gt; let genObj2 = genFunc2();
  583. &gt; genObj2.next()
  584. Started
  585. { value: undefined, done: false }
  586. &gt; genObj2.return('Result')
  587. { value: 'Not done, yet!', done: false }
  588. </pre>
  589. <p>You can invoke <code>next()</code> one more time. Similarly to non-generator functions, the return value of the generator function is the value that was queued prior to entering the <code>finally</code> clause.</p>
  590. <pre class="repl">
  591. &gt; genObj2.next()
  592. { value: 'Result', done: true }
  593. </pre>
  594. <h5 id="returning_from_a_newborn_generator">Returning from a newborn generator</h5><p>Returning a value from a <em>newborn</em> generator (that hasn’t started yet) is allowed:</p>
  595. <pre class="repl">
  596. &gt; function* genFunc() {}
  597. &gt; genFunc().return('yes')
  598. { value: 'yes', done: true }
  599. </pre>
  600. <h4 id="throw%28%29_signals_an_error"><code>throw()</code> signals an error</h4><p><code>throw()</code> throws an exception at the location of the <code>yield</code> that led to the last suspension of the generator. Let’s examine how that works via the following generator function.</p>
  601. <pre class="javascript">
  602. function* genFunc1() {
  603. try {
  604. console.log('Started');
  605. yield; // (A)
  606. } catch (error) {
  607. console.log('Caught: ' + error);
  608. }
  609. }
  610. </pre>
  611. <p>In the following interaction, we first use <code>next()</code> to start the generator and proceed until the <code>yield</code> in line (A). Then we throw an exception from that location.</p>
  612. <pre class="javascript">
  613. &gt; let genObj1 = genFunc1();
  614. &gt; genObj1.next()
  615. Started
  616. { value: undefined, done: false }
  617. &gt; genObj1.throw(new Error('Problem!'))
  618. Caught: Error: Problem!
  619. { value: undefined, done: true }
  620. </pre>
  621. <p>The result of <code>throw()</code> (shown in the last line) stems from us leaving the function with an implicit <code>return</code>.</p>
  622. <h5 id="uncaught_exceptions">Uncaught exceptions</h5><p>If you don’t catch the exception inside the generator, it is thrown by <code>throw()</code>. For example, the following generator function does not catch exceptions:</p>
  623. <pre class="javascript">
  624. function* genFunc2() {
  625. console.log('Started');
  626. yield; // (A)
  627. }
  628. </pre>
  629. <p>If we use <code>throw()</code> to throw an instance of <code>Error</code> at line (A), the method itself throws that error:</p>
  630. <pre class="repl">
  631. &gt; let genObj2 = genFunc2();
  632. &gt; genObj2.next()
  633. Started
  634. { value: undefined, done: false }
  635. &gt; genObj2.throw(new Error('Problem!'))
  636. Error: Problem!
  637. </pre>
  638. <h5 id="throwing_from_a_newborn_generator">Throwing from a newborn generator</h5><p>Throwing an exception in a <em>newborn</em> generator (that hasn’t started yet) is allowed:</p>
  639. <pre class="repl">
  640. &gt; function* genFunc() {}
  641. &gt; genFunc().throw(new Error('Problem!'))
  642. Error: Problem!
  643. </pre>
  644. <h4 id="example%3A_processing_asynchronously_pushed_data">Example: processing asynchronously pushed data</h4><p>The fact that generators-as-observers pause while they wait for input makes them perfect for on-demand processing of data that is received asynchronously. The pattern for setting up a chain of generators for processing is as follows:</p>
  645. <ul>
  646. <li><p>First chain member: a normal function that has a parameter <code>target</code>, which is the generator object of the next element in the chain of generators. The function makes an asynchronous request and pushes the results to the target via <code>target.next()</code>.</p>
  647. </li>
  648. <li><p>Intermediate chain members: generators that have a parameter <code>target</code>. They receive data via <code>yield</code> and send data via <code>target.next()</code>.</p>
  649. </li>
  650. <li><p>Last chain member: a generator that has no parameter <code>target</code> and only receives data.</p>
  651. </li>
  652. </ul>
  653. <p>As an example, let’s chain generators to process a file that is read asynchronously. The code shown in this section is a Node.js script that is executed via <code>babel-node</code> <a class="ptr">[8]</a>.</p>
  654. <p>The following code sets up the chain, which starts with the normal function <code>readFile</code>, continues with the generators <code>splitLines</code> and <code>numberLines</code> and ends with the generator <code>printLines</code>:</p>
  655. <pre class="javascript">
  656. let fileName = process.argv[2];
  657. readFile(fileName, chain(splitLines, numberLines, printLines));
  658. </pre>
  659. <p>I’ll explain what these functions do when I show their code. The following helper function sets up a chain of generators: Starting with the last generator, each generator function is called and the resulting generator object is used to start the generator via an initial <code>next()</code>. If a generator has a successor, it receives the successor’s generator object via the parameter <code>target</code>. The result of <code>chain()</code> is the generator object of the first generator function (in our example: <code>splitLines</code>).</p>
  660. <pre class="javascript">
  661. function chain(...generatorFuncs) {
  662. if (generatorFuncs.length &lt; 1) {
  663. throw new Error('Need at least 1 argument');
  664. }
  665. let generatorObject = generatorFuncs[generatorFuncs.length-1]();
  666. generatorObject.next(); // generator is now ready for input
  667. for (let i=generatorFuncs.length-2; i &gt;= 0; i--) {
  668. let generatorFunction = generatorFuncs[i];
  669. // Link current generator to successor
  670. generatorObject = generatorFunction(generatorObject);
  671. // Start the generator
  672. generatorObject.next();
  673. }
  674. return generatorObject;
  675. }
  676. </pre>
  677. <p><code>readFile()</code> is the non-generator function that starts everything.</p>
  678. <pre class="javascript">
  679. import {createReadStream} from 'fs';
  680. /**
  681. * Create an asynchronous ReadStream for the file whose name
  682. * is `fileName` and feed it to the generator object `target`.
  683. *
  684. * @see ReadStream https://nodejs.org/api/fs.html#fs_class_fs_readstream
  685. */
  686. function readFile(fileName, target) {
  687. let readStream = createReadStream(fileName,
  688. { encoding: 'utf8', bufferSize: 1024 });
  689. readStream.on('data', buffer =&gt; {
  690. let str = buffer.toString('utf8');
  691. target.next(str);
  692. });
  693. readStream.on('end', () =&gt; {
  694. // Signal end of output sequence
  695. target.return();
  696. });
  697. }
  698. </pre>
  699. <p>The chain of generators starts with <code>splitLines</code>:</p>
  700. <pre class="javascript">
  701. /**
  702. * Turns a sequence of text chunks into a sequence of lines
  703. * (where lines are separated by newlines)
  704. */
  705. function* splitLines(target) {
  706. let previous = '';
  707. try {
  708. while (true) {
  709. previous += yield;
  710. let eolIndex;
  711. while ((eolIndex = previous.indexOf('\n')) &gt;= 0) {
  712. let line = previous.slice(0, eolIndex);
  713. target.next(line);
  714. previous = previous.slice(eolIndex+1);
  715. }
  716. }
  717. } finally {
  718. // Handle the end of the input sequence
  719. // (signaled via `return()`)
  720. if (previous.length &gt; 0) {
  721. target.next(previous);
  722. }
  723. // Signal end of output sequence
  724. target.return();
  725. }
  726. }
  727. </pre>
  728. <p>Note an important pattern:</p>
  729. <ul>
  730. <li><code>readFile</code> uses the generator object method <code>return()</code> to signal the end of the sequence of chunks that it sends.</li>
  731. <li><code>readFile</code> sends that signal while <code>splitLines</code> is waiting for input via <code>yield</code>, inside an infinite loop. <code>return()</code> breaks from that loop.</li>
  732. <li><code>splitLines</code> uses a <code>finally</code> clause to handle the end-of-sequence.</li>
  733. </ul>
  734. <p>The next generator is <code>numberLines</code>:</p>
  735. <pre class="javascript">
  736. /**
  737. * Prefixes numbers to a sequence of lines
  738. */
  739. function* numberLines(target) {
  740. try {
  741. for (let lineNo = 0; ; lineNo++) {
  742. let line = yield;
  743. target.next(`${lineNo}: ${line}`);
  744. }
  745. } finally {
  746. // Signal end of output sequence
  747. target.return();
  748. }
  749. }
  750. </pre>
  751. <p>The last generator is <code>printLines</code>:</p>
  752. <pre class="javascript">
  753. /**
  754. * Receives a sequence of lines (without newlines)
  755. * and logs them (adding newlines).
  756. */
  757. function* printLines() {
  758. while (true) {
  759. let line = yield;
  760. console.log(line);
  761. }
  762. }
  763. </pre>
  764. <p>The neat thing about this code is that everything happens lazily (on demand): lines are split, numbered and printed as they arrive; we don’t have to wait for all of the text before we can start printing.</p>
  765. <h4 id="yield*%3A_the_full_story"><code>yield*</code>: the full story</h4><p>So far, we have only seen one aspect of <code>yield</code>: it propagates yielded values from the callee to the caller. Now that we are interested in generators receiving input, another aspect becomes relevant: <code>yield*</code> also forwards input received by the caller to the callee.</p>
  766. <p>I’ll first explain the complete semantics of <code>yield*</code> by showing how you’d implemented it in JavaScript. Then I give simple examples where input received by the caller via <code>next()</code>, <code>return()</code> and <code>throw()</code> is forwarded to the callee.</p>
  767. <p>The following statement:</p>
  768. <pre class="javascript">
  769. let yieldStarResult = yield* calleeFunc();
  770. </pre>
  771. <p>is roughly equivalent to:</p>
  772. <pre class="javascript">
  773. let yieldStarResult;
  774. let calleeObj = calleeFunc();
  775. let prevReceived = undefined;
  776. while (true) {
  777. try {
  778. // Forward input previously received
  779. let item = calleeObj.next(prevReceived);
  780. if (item.done) {
  781. yieldStarResult = item.value;
  782. break;
  783. }
  784. prevReceived = yield item.value;
  785. } catch (e) {
  786. // Pretend `return` can be caught like an exception
  787. if (e instanceof Return) {
  788. // Forward input received via return()
  789. calleeObj.return(e.returnedValue);
  790. return e.returnedValue; // “re-throw”
  791. } else {
  792. // Forward input received via throw()
  793. calleeObj.throw(e); // may throw
  794. }
  795. }
  796. }
  797. </pre>
  798. <p>To keep things simple, several things are missing in this code:</p>
  799. <ul>
  800. <li><p>The operand of <code>yield*</code> can be any iterable value.</p>
  801. </li>
  802. <li><p><code>return()</code> and <code>throw()</code> are optional iterator methods. We should only call them if they exist.</p>
  803. </li>
  804. <li><p>If an exception is received and <code>throw()</code> does not exist, but <code>return()</code> does then <code>return()</code> is called (before throwing an exception) to give <code>calleeObject</code> the opportunity to clean up.</p>
  805. </li>
  806. <li><p><code>calleeObj</code> can refuse to close, by returning an object whose property <code>done</code> is <code>false</code>. Then the caller also has to refuse to close and <code>yield*</code> must continue to iterate.</p>
  807. </li>
  808. </ul>
  809. <h5 id="example%3A_yield*_forwards_next%28%29">Example: <code>yield*</code> forwards <code>next()</code></h5><p>The following generator function <code>caller()</code> invokes the generator function <code>callee()</code> via <code>yield*</code>.</p>
  810. <pre class="javascript">
  811. function* callee() {
  812. console.log('callee: ' + (yield));
  813. }
  814. function* caller() {
  815. while (true) {
  816. yield* callee();
  817. }
  818. }
  819. </pre>
  820. <p><code>callee</code> logs values received via <code>next()</code>, which allows us to check whether it receives the value <code>'a'</code> and <code>'b'</code> that we send to <code>caller</code>.</p>
  821. <pre class="repl">
  822. &gt; let callerObj = caller();
  823. &gt; callerObj.next() // start
  824. { value: undefined, done: false }
  825. &gt; callerObj.next('a')
  826. callee: a
  827. { value: undefined, done: false }
  828. &gt; callerObj.next('b')
  829. callee: b
  830. { value: undefined, done: false }
  831. </pre>
  832. <h5 id="example%3A_yield*_forwards_throw%28%29">Example: <code>yield*</code> forwards <code>throw()</code></h5><p>Let’s use the following code to demonstrate how <code>throw()</code> works while <code>yield*</code> is delegating to another generator.</p>
  833. <pre class="javascript">
  834. function* callee() {
  835. try {
  836. yield 'b'; // (A)
  837. yield 'c';
  838. } finally {
  839. console.log('finally callee');
  840. }
  841. }
  842. function* caller() {
  843. try {
  844. yield 'a';
  845. yield* callee();
  846. yield 'd';
  847. } catch (e) {
  848. console.log('[caller] ' + e);
  849. }
  850. }
  851. </pre>
  852. <p>We first create a generator object and advance until line (A).</p>
  853. <pre class="repl">
  854. &gt; let genObj = caller();
  855. &gt; genObj.next().value
  856. 'a'
  857. &gt; genObj.next().value
  858. 'b'
  859. </pre>
  860. <p>In that line, we throw an exception:</p>
  861. <pre class="repl">
  862. &gt; genObj.throw(new Error('Problem!'))
  863. finally callee
  864. [caller] Error: Problem!
  865. { value: undefined, done: true }
  866. </pre>
  867. <p><code>callee</code> doesn’t catch the exception, which is why it is propagated into <code>caller</code>, where it is logged before <code>caller</code> finishes.</p>
  868. <h5 id="example%3A_yield*_forwards_return%28%29">Example: <code>yield*</code> forwards <code>return()</code></h5><p>Let’s use the following code to demonstrate how <code>return()</code> works while <code>yield*</code> is delegating to another generator.</p>
  869. <pre class="javascript">
  870. function* callee() {
  871. try {
  872. yield 'b';
  873. yield 'c';
  874. } finally {
  875. console.log('finally callee');
  876. }
  877. }
  878. function* caller() {
  879. try {
  880. yield 'a';
  881. yield* callee();
  882. yield 'd';
  883. } finally {
  884. console.log('finally caller');
  885. }
  886. }
  887. </pre>
  888. <p>Destructuring closes an iterator via <code>return()</code> if one doesn’t iterate until the end:</p>
  889. <pre class="javascript">
  890. let [x, y] = caller(); // ['a', 'b']
  891. // Output:
  892. // finally callee
  893. // finally caller
  894. </pre>
  895. <p>Interestingly, the <code>return()</code> is sent to <code>caller</code> and forwarded to <code>callee</code> (which it terminates early), but then also terminates <code>caller</code> (which is what someone invoking <code>return()</code> would expect). That is, <code>return</code> is propagated much like an exception.</p>
  896. <h5 id="how_to_think_about_yield*">How to think about <code>yield*</code></h5><p>There are two ways to think about <code>yield*</code>:</p>
  897. <ul>
  898. <li>As a function call from generator to generator.</li>
  899. <li>In order to understand <code>return()</code>, it helps to ask yourself: what should happen if I copy-pasted the code of the callee function into the code of the caller function.</li>
  900. </ul>
  901. <h3 id="generators_as_coroutines_%28cooperative_multitasking%29">Generators as coroutines (cooperative multitasking)</h3><p>We have seen generators being used as either sources or sinks of data. For many applications, it’s good practice to strictly separate these two roles, because it keeps things simpler. This section describes the full generator interface (which combines both roles) and one application where both roles are needed: cooperative multitasking, where tasks must be able to both send and receive information.</p>
  902. <h4 id="the_full_generator_interface">The full generator interface</h4><p>The full interface of generator objects, <code>Generator</code>, handles both output and input and combines two interfaces that we have seen previously: <code>Iterator</code> for output and <code>Observer</code> for input.</p>
  903. <pre class="javascript">
  904. interface Iterator { // data producer
  905. next() : IteratorResult;
  906. return?(value? : any) : IteratorResult;
  907. }
  908. interface Observer { // data consumer
  909. next(value? : any) : void;
  910. return(value? : any) : void;
  911. throw(error) : void;
  912. }
  913. </pre>
  914. <p>This is the full interface of generator objects (<a href="https://people.mozilla.org/~jorendorff/es6-draft.html#sec-properties-of-generator-prototype">as described by the ECMAScript language specification</a>):</p>
  915. <pre class="javascript">
  916. interface Generator {
  917. next(value? : any) : IteratorResult;
  918. throw(value? : any) : IteratorResult;
  919. return(value? : any) : IteratorResult;
  920. }
  921. interface IteratorResult {
  922. value : any;
  923. done : boolean;
  924. }
  925. </pre>
  926. <h4 id="cooperative_multitasking">Cooperative multitasking</h4><p>Cooperative multitasking is an application of generators where we need them to handle both output and input. Before we get into how that works, let’s first review the current state of parallelism in JavaScript <a class="ptr">[9]</a>.</p>
  927. <p>JavaScript runs in a single process. There are two ways in which this limitation is being abolished:</p>
  928. <ul>
  929. <li><p>Multiprocessing: <em>Web Workers</em> let you run JavaScript in multiple processes. Shared access to data is one of the biggest pitfalls of multiprocessing. Web Workers avoid it by not sharing any data. That is, if you want a Web Worker to have a piece of data, you must send it a copy or transfer your data to it (after which you can’t access it anymore).</p>
  930. </li>
  931. <li><p>Cooperative multitasking: There are various patterns and libraries that experiment with cooperative multitasking. Multiple tasks are run, but only one at a time. Each task must explicitly suspend itself, giving it full control over when a task switch happens. In these experiments, data is often shared between tasks. But due to explicit suspension, there are few risks.</p>
  932. </li>
  933. </ul>
  934. <p>Two use cases benefit from cooperative multitasking, because they involve control flows that are mostly sequential, anyway, with occasional pauses:</p>
  935. <ul>
  936. <li>Asynchronous computations: A task blocks (pauses) until it receives the result of a long-running computation.</li>
  937. <li>Streams: A task sequentially processes a stream of data and pauses if there is no data available.</li>
  938. </ul>
  939. <p>For binary streams, WHATWG is currently working on a <a href="https://streams.spec.whatwg.org/">standard proposal</a> that is based on callbacks and promises.</p>
  940. <p>For streams of data, Communicating Sequential Processes (CSP) are an interesting solution. A generator-based CSP library is covered later in this blog post.</p>
  941. <p>For asynchronous computations, Promises <a class="ptr">[10]</a> have become popular and are included in ECMAScript 6.</p>
  942. <h5 id="simplifying_asynchronous_code_via_generators">Simplifying asynchronous code via generators</h5><p>Several promise-based libraries simplify asynchronous code via generators. Generators are ideal as clients of promises, because they can be suspended until a result arrives.</p>
  943. <p>The following example demonstrates what that looks like if one uses <a href="https://github.com/tj/co">the library <em>co</em></a> by T.J. Holowaychuk. We need two libraries (if we run Node.js code via babel-node <a class="ptr">[8]</a>):</p>
  944. <pre class="javascript">
  945. require('isomorphic-fetch'); // polyfill
  946. let co = require('co');
  947. </pre>
  948. <p><code>co</code> is the actual library for cooperative multitasking, <code>isomorphic-fetch</code> is a polyfill for the new promise-based <code>fetch</code> API (a replacement of <code>XMLHttpRequest</code>; read “<a href="http://jakearchibald.com/2015/thats-so-fetch/">That's so fetch!</a>” by Jake Archibald for more information). <code>fetch</code> makes it easy to write a function <code>getFile</code> that returns the text of a file at a <code>url</code> via a Promise:</p>
  949. <pre class="javascript">
  950. function getFile(url) {
  951. return fetch(url)
  952. .then(request =&gt; request.text());
  953. }
  954. </pre>
  955. <p>We now have all the ingredients to use <code>co</code>. The following task reads the texts of two files, parses the JSON inside them and logs the result.</p>
  956. <pre class="javascript">
  957. co(function* () {
  958. try {
  959. let [croftStr, bondStr] = yield Promise.all([ // (A)
  960. getFile('http://localhost:8000/croft.json'),
  961. getFile('http://localhost:8000/bond.json'),
  962. ]);
  963. let croftJson = JSON.parse(croftStr);
  964. let bondJson = JSON.parse(bondStr);
  965. console.log(croftJson);
  966. console.log(bondJson);
  967. } catch (e) {
  968. console.log('Failure to read: ' + e);
  969. }
  970. });
  971. </pre>
  972. <p>Note how nicely synchronous this code looks, even though it makes an asynchronous call in line (A). A generator-as-task makes an async call by yielding a promise to the scheduler function <code>co</code>. The yielding pauses the generator. Once the promise returns a result, the scheduler resumes the generator by passing it the result via <code>next()</code>. A simple version of <code>co</code> looks as follows.</p>
  973. <pre class="javascript">
  974. function co(genFunc) {
  975. let genObj = genFunc();
  976. run();
  977. function run(promiseResult = undefined) {
  978. let item = genObj.next(promiseResult);
  979. if (!item.done) {
  980. // A promise was yielded
  981. item.value
  982. .then(result =&gt; run(result))
  983. .catch(error =&gt; {
  984. genObj.throw(error);
  985. });
  986. }
  987. }
  988. }
  989. </pre>
  990. <h4 id="the_limitations_of_cooperative_multitasking_via_generators">The limitations of cooperative multitasking via generators</h4><p><em>Coroutines</em> are cooperatively multitasked tasks that have no limitations: Inside a coroutine, any function can suspend the whole coroutine (the function activation itself, the activation of the function’s caller, the caller’s caller, etc.).</p>
  991. <p>In contrast, you can only suspend a generator from directly within a generator and only the current function activation is suspended. Due to these limitations, generators are occasionally called <em>shallow coroutines</em> <a class="ptr">[3]</a>.</p>
  992. <h5 id="the_benefits_of_the_limitations_of_generators">The benefits of the limitations of generators</h5><p>The limitations of generators have two main benefits:</p>
  993. <ul>
  994. <li><p>Generators are compatible with <em>event loops</em>, which provide simple cooperative multitasking in browsers. I’ll explain the details momentarily.</p>
  995. </li>
  996. <li><p>Generators are relatively easy to implement, because only a single function activation needs to be suspended and because browsers can continue to use event loops.</p>
  997. </li>
  998. </ul>
  999. <p>JavaScript already has a very simple style of cooperative multitasking: the event loop <a class="ptr">[9]</a>, which schedules the execution of tasks in a queue. Each task is started by calling a function and finished once that function is finished (that’s a simplification of how things actually work, but it’ll do for now). Events, <code>setTimeout()</code> and other mechanisms add tasks to the queue.</p>
  1000. <p>This style of multitasking makes one important guarantee: <em>run to completion</em>; every function can rely on not being interrupted by another task until it is finished. Functions become transactions and can perform complete algorithms without anyone seeing the data they operate on in an itermediate state. Concurrent access to shared data makes multitasking complicated and is not allowed by JavaScript’s concurrency model. That’s why run to completion is a good thing.</p>
  1001. <p>Alas, coroutines prevent run to completion, because any function could suspend its caller. For example, the following algorithm consists of multiple steps:</p>
  1002. <pre class="undefined">
  1003. step1(sharedData);
  1004. step2(sharedData);
  1005. lastStep(sharedData);
  1006. </pre>
  1007. <p>If <code>step2</code> was to suspend the algorithm, other tasks could run before the last step of the algorithm is performed. Those tasks could contain other parts of the application which would see <code>sharedData</code> in an unfinished state.</p>
  1008. <p>Generators preserve run to completion, they only suspend themselves and return to their caller. <code>co</code> and similar libraries give you most of the power of coroutines:</p>
  1009. <ul>
  1010. <li>They provide schedulers for tasks defined via generators.</li>
  1011. <li>Tasks start with generators and can thus be fully suspended.</li>
  1012. <li>A recursive (generator) function call is only suspendable if it is done via <code>yield*</code>. That gives callers control over suspension.</li>
  1013. </ul>
  1014. <h3 id="examples_of_generators">Examples of generators</h3><p>This section gives several examples of what generators can be used for.</p>
  1015. <h4 id="implementing_iterables_via_generators_2">Implementing iterables via generators</h4><p>In the blog post “<a href="http://www.2ality.com/2015/02/es6-iteration.html">Iterables and iterators in ECMAScript 6</a>”, I implemented several iterables “by hand”. In this section, I use generators, instead.</p>
  1016. <h5 id="the_iterable_combinator_take%28%29">The iterable combinator <code>take()</code></h5><p><code>take()</code> converts a (potentially infinite) sequence of iterated values into a sequence of length <code>n</code>:</p>
  1017. <pre class="javascript">
  1018. function* take(n, iterable) {
  1019. for (let x of iterable) {
  1020. if (n &lt;= 0) return;
  1021. n--;
  1022. yield x;
  1023. }
  1024. }
  1025. </pre>
  1026. <p>The following is an example of using it:</p>
  1027. <pre class="javascript">
  1028. let arr = ['a', 'b', 'c', 'd'];
  1029. for (let x of take(2, arr)) {
  1030. console.log(x);
  1031. }
  1032. // Output:
  1033. // a
  1034. // b
  1035. </pre>
  1036. <p>An implementation of <code>take()</code> without generators is more complicated:</p>
  1037. <pre class="javascript">
  1038. function take(n, iterable) {
  1039. let iter = iterable[Symbol.iterator]();
  1040. return {
  1041. [Symbol.iterator]() {
  1042. return this;
  1043. },
  1044. next() {
  1045. if (n &gt; 0) {
  1046. n--;
  1047. return iter.next();
  1048. } else {
  1049. iter.return()
  1050. return { done: true };
  1051. }
  1052. },
  1053. return() {
  1054. n = 0;
  1055. iter.return();
  1056. }
  1057. };
  1058. }
  1059. </pre>
  1060. <p>Note that the iterable combinator <code>zip()</code> does not profit much from being implemented via a generator, because multiple iterables are involved (and <code>for-of</code> can’t be used).</p>
  1061. <h5 id="infinite_iterables">Infinite iterables</h5><p><code>naturalNumbers()</code> returns an iterable over all natural numbers:</p>
  1062. <pre class="javascript">
  1063. function* naturalNumbers() {
  1064. for (let n=0;; n++) {
  1065. yield n;
  1066. }
  1067. }
  1068. </pre>
  1069. <p>This function is often used in conjunction with a combinator:</p>
  1070. <pre class="javascript">
  1071. for (let x of take(3, naturalNumbers())) {
  1072. console.log(x);
  1073. }
  1074. // Output
  1075. // 0
  1076. // 1
  1077. // 2
  1078. </pre>
  1079. <p>One last time, I show the non-generator implementation, so you can compare:</p>
  1080. <pre class="javascript">
  1081. function naturalNumbers() {
  1082. let n = 0;
  1083. return {
  1084. [Symbol.iterator]() {
  1085. return this;
  1086. },
  1087. next() {
  1088. return { value: n++ };
  1089. }
  1090. }
  1091. }
  1092. </pre>
  1093. <h5 id="array-inspired_iterable_combinators%3A_map_filter">Array-inspired iterable combinators: <code>map</code>, <code>filter</code></h5><p>Arrays can be transformed via the methods <code>map</code> and <code>filter</code>. Those methods can be generalized to have iterables as input and iterables as output.</p>
  1094. <p>This is the generalized version of <code>map</code>:</p>
  1095. <pre class="javascript">
  1096. function* map(iterable, mapFunc) {
  1097. for (let x of iterable) {
  1098. yield mapFunc(x);
  1099. }
  1100. }
  1101. // Works with infinite iterables!
  1102. let arr = [...take(4, map(naturalNumbers(), x =&gt; x * x))];
  1103. // [0, 1, 4, 9]
  1104. </pre>
  1105. <p>This is the generalized version of <code>filter</code>:</p>
  1106. <pre class="javascript">
  1107. function* filter(iterable, filterFunc) {
  1108. for (let x of iterable) {
  1109. if (filterFunc(x)) {
  1110. yield x;
  1111. }
  1112. }
  1113. }
  1114. // Works with infinite iterables!
  1115. let arr = [...take(4, filter(naturalNumbers(), x =&gt; (x % 2) === 0))];
  1116. // [0, 2, 4, 6]
  1117. </pre>
  1118. <h4 id="generators_for_lazy_evaluation">Generators for lazy evaluation</h4><p>The next two examples show how generators can be used to process a stream of characters.</p>
  1119. <ul>
  1120. <li><p>The input is a stream of characters.</p>
  1121. </li>
  1122. <li><p>Step 1 – tokenize (characters → words): The characters are grouped into <em>words</em>, strings that match the regular expression <code>/^[A-Za-z0-9]$/</code>. Non-word characters are ignored, but they separate words. The input of this step is a stream of characters, the output a stream of words.</p>
  1123. </li>
  1124. <li><p>Step 2 – extract numbers (words → numbers): only keep words that match the regular expression <code>/^[0-9]+$/</code> and convert them to numbers.</p>
  1125. </li>
  1126. <li><p>Step 3 – add numbers (numbers → number): produce a single number by computing the total of all numbers in a stream.</p>
  1127. </li>
  1128. </ul>
  1129. <p>The neat thing is that everything is computed <em>lazily</em> (incrementally and on demand): computation starts as soon as the first character arrives. For example, we don’t have to wait until we have all characters to get the first word.</p>
  1130. <h5 id="lazy_pull_%28generators_as_iterators%29">Lazy pull (generators as iterators)</h5><p><strong>Step 1 – tokenize.</strong> The following trick makes the code a bit simpler: the end-of-sequence iterator result (whose property <code>done</code> is <code>false</code>) is converted into the sentinel value <code>END_OF_SEQUENCE</code>.</p>
  1131. <pre class="javascript">
  1132. function* tokenize(chars) {
  1133. let iterator = chars[Symbol.iterator]();
  1134. let ch;
  1135. do {
  1136. ch = getNextItem(iterator);
  1137. if (isWordChar(ch)) {
  1138. let word = '';
  1139. do {
  1140. word += ch;
  1141. ch = getNextItem(iterator);
  1142. } while (isWordChar(ch));
  1143. yield word;
  1144. }
  1145. // Ignore all other characters
  1146. } while (ch !== END_OF_SEQUENCE);
  1147. }
  1148. const END_OF_SEQUENCE = Symbol();
  1149. function getNextItem(iterator) {
  1150. let item = iterator.next();
  1151. return item.done ? END_OF_SEQUENCE : item.value;
  1152. }
  1153. function isWordChar(ch) {
  1154. return typeof ch === 'string' &amp;&amp; /^[A-Za-z0-9]$/.test(ch);
  1155. }
  1156. </pre>
  1157. <p>Let’s try out tokenization. Note that the spaces and the dot are non-words. They are ignored, but they separate words. We use the fact that strings are iterables over characters (Unicode code points). The final result is an iterable over words, which we turn into an array via the spread operator (<code>...</code>).</p>
  1158. <pre class="repl">
  1159. &gt; [...tokenize('2 apples and 5 oranges.')]
  1160. [ '2', 'apples', 'and', '5', 'oranges' ]
  1161. </pre>
  1162. <p><strong>Step 2 – extract numbers:</strong> This step is relatively simple, we only <code>yield</code> words that contain nothing but digits, after converting them to numbers via <code>Number()</code>.</p>
  1163. <pre class="javascript">
  1164. function* extractNumbers(words) {
  1165. for (let word of words) {
  1166. if (/^[0-9]+$/.test(word)) {
  1167. yield Number(word);
  1168. }
  1169. }
  1170. }
  1171. </pre>
  1172. <p>The following example shows the transformation steps: characters → words → numbers.</p>
  1173. <pre class="repl">
  1174. &gt; [...extractNumbers(tokenize('2 apples and 5 oranges.'))]
  1175. [ 2, 5 ]
  1176. </pre>
  1177. <p><strong>Step 3 – add numbers:</strong> This last step is performed by a normal function that pulls the results and reports their total.</p>
  1178. <pre class="javascript">
  1179. function summarize(numbers) {
  1180. let result = 0;
  1181. for (let n of numbers) {
  1182. result += n;
  1183. }
  1184. return result;
  1185. }
  1186. </pre>
  1187. <p>The final result shows us that there are 7 things in the input sentence.</p>
  1188. <pre class="repl">
  1189. &gt; summarize(extractNumbers(tokenize('2 apples and 5 oranges.')))
  1190. 7
  1191. </pre>
  1192. <h5 id="lazy_push_%28generators_as_observables%29">Lazy push (generators as observables)</h5><p>Not much work is needed to convert the previous pull-based algorithm into a push-based one. The steps are the same. But instead of finishing via pulling, we start via pushing (in both cases, non-generator functions are used).</p>
  1193. <p>As previously explained, if generators receive input via <code>yield</code>, the first invocation of <code>next()</code> on the generator object doesn’t do anything. That’s why we again use the following helper function:</p>
  1194. <pre class="javascript">
  1195. /**
  1196. * Returns a function that, when called,
  1197. * returns a generator object that is immediately
  1198. * ready for input via `next()`
  1199. */
  1200. function coroutine(generatorFunction) {
  1201. return function (...args) {
  1202. let generatorObject = generatorFunction(...args);
  1203. generatorObject.next();
  1204. return generatorObject;
  1205. };
  1206. }
  1207. </pre>
  1208. <p>The following function <code>send()</code> wraps the chain of generators. Its parameter <code>receiver</code> holds the generator object of the first generator in the chain. <code>send()</code> pushes the contents of <code>iterable</code> to the <code>receiver</code>, via <code>next()</code>.</p>
  1209. <pre class="javascript">
  1210. function send(iterable, receiver) {
  1211. for (let x of iterable) {
  1212. receiver.next(x);
  1213. }
  1214. receiver.return(); // signal end of stream
  1215. }
  1216. </pre>
  1217. <p>When a generator processes a stream, it needs to be aware of the end of the stream, so that it can clean up properly. For pull, we did this via a special end-of-stream sentinel. For push, the end-of-stream is signaled via <code>return()</code>.</p>
  1218. <p>Let’s test <code>send()</code> via a generator that simply outputs everything it receives:</p>
  1219. <pre class="javascript">
  1220. const logItems = coroutine(function* () {
  1221. try {
  1222. while (true) {
  1223. let item = yield;
  1224. console.log(item);
  1225. }
  1226. } finally {
  1227. console.log('DONE');
  1228. }
  1229. });
  1230. </pre>
  1231. <p>Let’s send <code>logItems()</code> three “characters” via a string (which is an iterable over Unicode code points).</p>
  1232. <pre class="repl">
  1233. &gt; send('abc', logItems());
  1234. a
  1235. b
  1236. c
  1237. DONE
  1238. </pre>
  1239. <p><strong>Step 1 – tokenize.</strong> Note how this generator reacts to the end of the stream (as signaled via <code>return()</code>) in two <code>finally</code> clauses. We depend on <code>return()</code> being sent to either one of the two <code>yield</code>s. Otherwise, the generator would never terminate, because the infinite loop starting in line (A) would never terminate.</p>
  1240. <pre class="javascript">
  1241. const tokenize = coroutine(function* (receiver) {
  1242. try {
  1243. while (true) { // (A)
  1244. let ch = yield;
  1245. if (isWordChar(ch)) {
  1246. // A word has started
  1247. let word = '';
  1248. try {
  1249. do {
  1250. word += ch;
  1251. ch = yield;
  1252. } while (isWordChar(ch));
  1253. } finally {
  1254. // The word is finished.
  1255. // We get here if
  1256. // (a) the loop terminates normally
  1257. // (b) the loop is terminated via `return()`
  1258. receiver.next(word);
  1259. }
  1260. }
  1261. // Ignore all other characters
  1262. }
  1263. } finally {
  1264. // We only get here if the infinite loop is terminated
  1265. // via `return()`. Forward `return()` to `receiver` so
  1266. // that it is also aware of the end of stream.
  1267. receiver.return();
  1268. }
  1269. });
  1270. function isWordChar(ch) {
  1271. return /^[A-Za-z0-9]$/.test(ch);
  1272. }
  1273. </pre>
  1274. <p><code>tokenize()</code> demonstrates that generators work well as implementations of linear state machines. In this case, the machine has two states: “inside a word” and “not inside a word”.</p>
  1275. <p>Let’s tokenize a string:</p>
  1276. <pre class="repl">
  1277. &gt; send('2 apples and 5 oranges.', tokenize(logItems()));
  1278. 2
  1279. apples
  1280. and
  1281. 5
  1282. oranges
  1283. </pre>
  1284. <p><strong>Step 2 – extract numbers:</strong> This step is straightforward.</p>
  1285. <pre class="javascript">
  1286. const extractNumbers = coroutine(function* (receiver) {
  1287. try {
  1288. while (true) {
  1289. let word = yield;
  1290. if (/^[0-9]+$/.test(word)) {
  1291. receiver.next(Number(word));
  1292. }
  1293. }
  1294. } finally {
  1295. // Only reached via `return()`, forward.
  1296. receiver.return();
  1297. }
  1298. });
  1299. </pre>
  1300. <p>Let’s log the numbers appearing in a string:</p>
  1301. <pre class="repl">
  1302. &gt; send('2 apples and 5 oranges.', tokenize(extractNumbers(logItems())));
  1303. 2
  1304. 5
  1305. DONE
  1306. </pre>
  1307. <p><strong>Step 3 – add numbers:</strong> This time, we react to the end of the stream by pushing a single value and then closing the receiver.</p>
  1308. <pre class="javascript">
  1309. const addNumbers = coroutine(function* (receiver) {
  1310. let result = 0;
  1311. try {
  1312. while (true) {
  1313. result += yield;
  1314. }
  1315. } finally {
  1316. // We received an end-of-stream
  1317. receiver.next(result);
  1318. receiver.return(); // signal end of stream
  1319. }
  1320. });
  1321. </pre>
  1322. <p>Let’s sum up the numbers appearing inside a string:</p>
  1323. <pre class="repl">
  1324. &gt; send('2 apples and 5 oranges.', tokenize(extractNumbers(addNumbers(logItems()))));
  1325. 7
  1326. DONE
  1327. </pre>
  1328. <p>As mentioned before, one benefit of a push-based approach is that it allows you to process data that you receive asynchronously.</p>
  1329. <h4 id="cooperative_multi-tasking_via_generators">Cooperative multi-tasking via generators</h4><h5 id="pausing_long-running_tasks">Pausing long-running tasks</h5><p>In this example, we create a counter that is displayed on a web page. We improve an initial version until we have a cooperatively multitasked version that doesn’t block the main thread and the user interface.</p>
  1330. <p>This is the part of the web page in which the counter should be displayed:</p>
  1331. <pre class="html">
  1332. &lt;body&gt;
  1333. Counter: &lt;span id="counter"&gt;&lt;/span&gt;
  1334. &lt;/body&gt;
  1335. </pre>
  1336. <p>This function displays a counter that counts up forever (well, until the number overflows):</p>
  1337. <pre class="javascript">
  1338. function countUp(start = 0) {
  1339. const counterSpan = document.querySelector('#counter');
  1340. while (true) {
  1341. counterSpan.textContent = String(start);
  1342. start++;
  1343. }
  1344. }
  1345. </pre>
  1346. <p>If you ran this function, it would completely block the user interface thread in which it runs and its tab would become unresponsive.</p>
  1347. <p>Let’s implement the same functionality via a generator that periodically pauses via <code>yield</code> (a scheduling function for running this generator is shown at the end):</p>
  1348. <pre class="javascript">
  1349. function* countUp(start = 0) {
  1350. const counterSpan = document.querySelector('#counter');
  1351. while (true) {
  1352. counterSpan.textContent = String(start);
  1353. start++;
  1354. yield; // pause
  1355. }
  1356. }
  1357. </pre>
  1358. <p>Let’s add one small improvement. We move the update of the user interface to another generator, <code>displayCounter</code>, which we call via <code>yield*</code>. As it is a generator, it can also take care of pausing.</p>
  1359. <pre class="javascript">
  1360. function* countUp(start = 0) {
  1361. while (true) {
  1362. start++;
  1363. yield* displayCounter(start);
  1364. }
  1365. }
  1366. function* displayCounter(counter) {
  1367. const counterSpan = document.querySelector('#counter');
  1368. counterSpan.textContent = String(counter);
  1369. yield; // pause
  1370. }
  1371. </pre>
  1372. <p>Lastly, this is a scheduling function that we can use to run <code>countUp()</code>. Each execution step of the generator is handled by a separate task, which is created via <code>setTimeout()</code>. That means that the user interface can schedule other tasks in between and will remain responsive.</p>
  1373. <pre class="javascript">
  1374. function run(generatorObject) {
  1375. if (!generatorObject.next().done) {
  1376. // Add a new task to the event queue
  1377. setTimeout(function () {
  1378. run(generatorObject);
  1379. }, 1000);
  1380. }
  1381. }
  1382. </pre>
  1383. <p>With the help of <code>run</code>, we get a (nearly) infinite count-up that doesn’t block the user interface:</p>
  1384. <pre class="javascript">
  1385. run(countUp());
  1386. </pre>
  1387. <h5 id="cooperative_multitasking_with_generators_and_node.js-style_callbacks">Cooperative multitasking with generators and Node.js-style callbacks</h5><p>If you call a generator function (or method), it does not have access to its generator object; its <code>this</code> is the <code>this</code> it would have if it were a non-generator function. A work-around is to pass the generator object into the generator function via <code>yield</code>.</p>
  1388. <p>The following Node.js script uses this technique, but wraps the generator object in a callback (<code>next</code>, line (A)). It must be run via <code>babel-node</code> <a class="ptr">[8]</a>.</p>
  1389. <pre class="javascript">
  1390. import {readFile} from 'fs';
  1391. let fileNames = process.argv.slice(2);
  1392. console.log(fileNames, readFile);
  1393. run(function* () {
  1394. let next = yield; // (A)
  1395. for (let f of fileNames) {
  1396. let contents = yield readFile(f, { encoding: 'utf8' }, next);
  1397. console.log('-------------', f);
  1398. console.log(contents);
  1399. }
  1400. });
  1401. </pre>
  1402. <p>In line (A), we get a callback that we can use with functions that follow Node.js callback conventions. The callback uses the generator object to wake up the generator, as you can see in the implementation of <code>run()</code>:</p>
  1403. <pre class="javascript">
  1404. function run(generatorFunction) {
  1405. let generatorObject = generatorFunction();
  1406. // Step 1: Proceed to first `yield`
  1407. generatorObject.next();
  1408. // Step 2: Pass in a function that the generator can use as a callback
  1409. let nextFunction = createNextFunction(generatorObject);
  1410. generatorObject.next(nextFunction);
  1411. // Subsequent invocations of `next()` are triggered by `nextFunction`
  1412. }
  1413. function createNextFunction(generatorObject) {
  1414. return function(error, result) {
  1415. if (error) {
  1416. generatorObject.throw(error);
  1417. } else {
  1418. generatorObject.next(result);
  1419. }
  1420. };
  1421. }
  1422. </pre>
  1423. <h5 id="communicating_sequential_processes_%28csp%29">Communicating Sequential Processes (CSP)</h5><p>The library <a href="https://github.com/ubolonton/js-csp"><code>js-csp</code></a> brings Communicating Sequential Processes (CSP) to JavaScript, a style of cooperative multitasking that is similar to ClojureScript’s core.async and Go’s <em>goroutines</em>. <code>js-csp</code> has two abstractions:</p>
  1424. <ul>
  1425. <li>Processes: are cooperatively multitasked tasks and implemented by handing a generator function to the scheduling function <code>go()</code>.</li>
  1426. <li>Channels: are queues for communication between processes. Channels are created by calling <code>chan()</code>.</li>
  1427. </ul>
  1428. <p>As an example, let’s use CSP to handle DOM events, in a manner reminiscent of Functional Reactive Programming. The following code uses the function <code>listen()</code> (which is shown later) to create a channel that outputs <code>mousemove</code> events. It then continuously retrieves the output via <code>take</code>, inside an infinite loop. Thanks to <code>yield</code>, the process blocks until the channel has output.</p>
  1429. <pre class="javascript">
  1430. import csp from 'js-csp';
  1431. csp.go(function* () {
  1432. let element = document.querySelector('#uiElement1');
  1433. let channel = listen(element, 'mousemove');
  1434. while (true) {
  1435. let event = yield csp.take(channel);
  1436. let x = event.layerX || event.clientX;
  1437. let y = event.layerY || event.clientY;
  1438. element.textContent = `${x}, ${y}`;
  1439. }
  1440. });
  1441. </pre>
  1442. <p><code>listen()</code> is implemented as follows.</p>
  1443. <pre class="javascript">
  1444. function listen(element, type) {
  1445. let channel = csp.chan();
  1446. element.addEventListener(type,
  1447. event =&gt; {
  1448. csp.putAsync(channel, event);
  1449. });
  1450. return channel;
  1451. }
  1452. </pre>
  1453. <p>This example is taken from the blog post “<a href="http://jlongster.com/Taming-the-Asynchronous-Beast-with-CSP-in-JavaScript">Taming the Asynchronous Beast with CSP Channels in JavaScript</a>” by James Long. Consult this blog post for more information on CSP.</p>
  1454. <h3 id="inheritance">Inheritance</h3><p>This is a diagram of how various objects are connected in ECMAScript 6 (it is based on <a href="https://people.mozilla.org/~jorendorff/es6-draft.html#sec-generatorfunction-objects">Allen Wirf-Brock’s diagram</a> in the ECMAScript specification):</p>
  1455. <div class="separator">
  1456. <a href="http://3.bp.blogspot.com/-kGgpHT0wjqU/VRkKCblRpRI/AAAAAAAAA70/BuiA8d-aZto/s1600/generator_inheritance.jpg" imageanchor="1"><img border="0" src="http://3.bp.blogspot.com/-kGgpHT0wjqU/VRkKCblRpRI/AAAAAAAAA70/BuiA8d-aZto/s1600/generator_inheritance.jpg"/></a></div>
  1457. <p>Legend:</p>
  1458. <ul>
  1459. <li>The white (hollow) arrows express the has-prototype relationship (inheritance) between objects. In other words: a white arrow from <code>x</code> to <code>y</code> means that <code>Object.getPrototypeOf(x) === y</code>.</li>
  1460. <li>Parentheses indicate that an object exists, but is not accessible via a global variable.</li>
  1461. <li>An <code>instanceof</code> arrow from <code>x</code> to <code>y</code> means that <code>x instanceof y</code>.<ul>
  1462. <li>Remember that <code>o instanceof C</code> is equivalent to <code>C.prototype.isPrototypeOf(o)</code>.</li>
  1463. </ul>
  1464. </li>
  1465. <li>A <code>prototype</code> arrow from <code>x</code> to <code>y</code> means that <code>x.prototype === y</code>.</li>
  1466. </ul>
  1467. <p>The diagram reveals two interesting facts:</p>
  1468. <p>First, a generator function <code>g</code> works very much like a constructor (you can even invoke it via <code>new</code>): The generator objects it creates are instances of it, methods added to <code>g.prototype</code> become prototype methods, etc.:</p>
  1469. <pre class="javascript">
  1470. &gt; function* g() {}
  1471. &gt; g.prototype.hello = function () { return 'hi!'};
  1472. &gt; let obj = g();
  1473. &gt; obj instanceof g
  1474. true
  1475. &gt; obj.hello()
  1476. 'hi!'
  1477. </pre>
  1478. <p>Second, if you want to make methods available for all generator objects, it’s best to add them to <code>(Generator.object)</code>. One way of accessing that object is as follows:</p>
  1479. <pre class="javascript">
  1480. &gt; let Generator_prototype = Object.getPrototypeOf(function* () {}).prototype;
  1481. &gt; Generator_prototype.hello = function () { return 'hi!'};
  1482. &gt; let generatorObject = (function* () {})();
  1483. &gt; generatorObject.hello()
  1484. 'hi!'
  1485. </pre>
  1486. <h4 id="iteratorprototype"><code>IteratorPrototype</code></h4><p>There is no <code>(Iterator)</code> in the diagram, because no such object exists. But, given how <code>instanceof</code> works and because <code>(IteratorPrototype)</code> is a prototype of <code>g1()</code>, you could still say that <code>g1()</code> is an instance of <code>Iterator</code>.</p>
  1487. <p>All iterators in ES6 have <code>(IteratorPrototype)</code> in their prototype chain. That object is iterable, because it has the following method. Therefore, all ES6 iterators are iterable (as a consequence, you can apply <code>for-of</code> etc. to them).</p>
  1488. <pre class="javascript">
  1489. [Symbol.iterator]() {
  1490. return this;
  1491. }
  1492. </pre>
  1493. <p>The specification recommends to use the following code to access <code>(IteratorPrototype)</code>:</p>
  1494. <pre class="javascript">
  1495. const proto = Object.getPrototypeOf.bind(Object);
  1496. let IteratorPrototype = proto(proto([][Symbol.iterator]()));
  1497. </pre>
  1498. <p>You could also use:</p>
  1499. <pre class="javascript">
  1500. let IteratorPrototype = proto(proto(function* () {}.prototype));
  1501. </pre>
  1502. <p>Quoting the ECMAScript 6 specification:</p>
  1503. <blockquote>
  1504. <p>ECMAScript code may also define objects that inherit from <code>IteratorPrototype</code>. The <code>IteratorPrototype</code> object provides a place where additional methods that are applicable to all iterator objects may be added.</p>
  1505. </blockquote>
  1506. <p><code>IteratorPrototype</code> will probably become directly accessible in an upcoming version of ECMAScript and contain tool methods such as <code>map()</code> and <code>filter()</code> (<a href="https://github.com/rwaldron/tc39-notes/blob/master/es6/2014-07/jul-30.md#47-revisit-comprehension-decision-from-last-meeting">source</a>).</p>
  1507. <h4 id="the_value_of_this_in_generators">The value of <code>this</code> in generators</h4><p>A generator function combines two concerns:</p>
  1508. <ol>
  1509. <li>It is a function that sets up and returns a generator object.</li>
  1510. <li>It contains the code that the generator object steps through.</li>
  1511. </ol>
  1512. <p>That’s why it’s not immediately obvious what the value of <code>this</code> should be inside a generator.</p>
  1513. <p>In function calls and method calls, <code>this</code> is what it would be if <code>gen()</code> wasn’t a generator function, but a normal function:</p>
  1514. <pre class="javascript">
  1515. function* gen() {
  1516. 'use strict'; // just in case
  1517. yield this;
  1518. }
  1519. // Retrieve the yielded value via destructuring
  1520. let [functionThis] = gen();
  1521. console.log(functionThis); // undefined
  1522. let obj = { method: gen };
  1523. let [methodThis] = obj.method();
  1524. console.log(methodThis === obj); // true
  1525. </pre>
  1526. <p>If you access <code>this</code> in a generator that was invoked via <code>new</code>, you get a <code>ReferenceError</code> (<a href="https://people.mozilla.org/~jorendorff/es6-draft.html#sec-generator-function-definitions-runtime-semantics-evaluatebody">source: ES6 spec</a>):</p>
  1527. <pre class="javascript">
  1528. function* gen() {
  1529. console.log(this); // ReferenceError
  1530. }
  1531. new gen();
  1532. </pre>
  1533. <p>We have previously seen a simple work-around: wrap the generator in a normal function that hands the generator its generator object via <code>yield</code>.</p>
  1534. <h3 id="style_consideration%3A_whitespace_before_and_after_the_asterisk">Style consideration: whitespace before and after the asterisk</h3><p>Reasonable – and legal – variations of formatting the asterisk are:</p>
  1535. <ul>
  1536. <li><p>A space before and after it:<br/><code>function * foo(x, y) { ... }</code></p>
  1537. </li>
  1538. <li><p>A space before it:<br/><code>function *foo(x, y) { ... }</code></p>
  1539. </li>
  1540. <li><p>A space after it:<br/><code>function* foo(x, y) { ... }</code></p>
  1541. </li>
  1542. <li><p>No whitespace before and after it:<br/><code>function*foo(x, y) { ... }</code></p>
  1543. </li>
  1544. </ul>
  1545. <p>Let’s figure out which of these variations make sense for which constructs and why.</p>
  1546. <h4 id="generator_function_declarations_and_expressions">Generator function declarations and expressions</h4><p>Here, the star is only used because <code>generator</code> (or something similar) isn’t available as a keyword. If it were, then a generator function declaration would look like this:</p>
  1547. <pre class="javascript">
  1548. generator foo(x, y) {
  1549. ...
  1550. }
  1551. </pre>
  1552. <p>Instead of <code>generator</code>, ECMAScript 6 marks the <code>function</code> keyword with an asterisk. Thus, <code>function*</code> can be seen as a synonym for <code>generator</code>, which suggests writing generator function declarations as follows.</p>
  1553. <pre class="javascript">
  1554. function* foo(x, y) {
  1555. ...
  1556. }
  1557. </pre>
  1558. <p>Anonymous generator functions would be formatted like this:</p>
  1559. <pre class="javascript">
  1560. const foo = function* (x, y) {
  1561. ...
  1562. }
  1563. </pre>
  1564. <h4 id="concise_generator_method_definitions">Concise generator method definitions</h4><p>When writing a concise generator method definitions, I recommend to format the asterisk as follows.</p>
  1565. <pre class="javascript">
  1566. let obj = {
  1567. * generatorMethod(x, y) {
  1568. ...
  1569. }
  1570. };
  1571. </pre>
  1572. <p>There are three arguments in favor of writing a space after the asterisk.</p>
  1573. <p>First, the asterisk shouldn’t be part of the method name. On one hand, it isn’t part of the name of a generator function. On the other hand, the asterisk is only mentioned when defining a generator, not when using it.</p>
  1574. <p>Second, a concise generator method definition is an abbreviation for the following syntax. (To make my point, I’m redundantly giving the function expression a name, too.)</p>
  1575. <pre class="javascript">
  1576. let obj = {
  1577. generatorMethod: function* generatorMethod(x, y) {
  1578. ...
  1579. }
  1580. };
  1581. </pre>
  1582. <p>If concise method definitions are about omitting the <code>function</code> keyword then the asterisk should probably be followed by a space.</p>
  1583. <p>Third, generator method definitions are syntactically similar to getters and setters (which are already available in ECMAScript 5):</p>
  1584. <pre class="javascript">
  1585. let obj = {
  1586. get foo() {
  1587. ...
  1588. }
  1589. set foo(value) {
  1590. ...
  1591. }
  1592. }
  1593. </pre>
  1594. <p>The keywords <code>get</code> and <code>set</code> can be seen as modifiers of a normal concise method definition. Arguably, an asterisk is also such a modifier.</p>
  1595. <h4 id="recursive_yield">Recursive <code>yield</code></h4><p>The following is an example of a generator function yielding its own yielded values recursively:</p>
  1596. <pre class="javascript">
  1597. function* foo(x) {
  1598. ...
  1599. yield* foo(x - 1);
  1600. ...
  1601. }
  1602. </pre>
  1603. <p>The asterisk marks a different kind of <code>yield</code> operator, which is why the above way of writing it makes sense.</p>
  1604. <h4 id="documenting_generator_functions_and_methods">Documenting generator functions and methods</h4><p>Kyle Simpson (@getify) proposed something interesting: Given that we often append parentheses when we write about functions and methods such as <code>Math.max()</code>, wouldn’t it make sense to prepend an asterisk when writing about generator functions and methods? For example: should we write <code>*foo()</code> to refer to the generator function in the previous subsection? Let me argue against that.</p>
  1605. <p>When it comes to writing a function that returns an iterable, a generator is only one of the several options. I think it is better to not give away this implementation detail via marked function names.</p>
  1606. <p>Furthermore, you don’t use the asterisk when calling a generator function, but you do use parentheses.</p>
  1607. <p>Lastly, the asterisk doesn’t provide useful information – <code>yield*</code> can also be used with functions that return an iterable. But it may make sense to mark the names of functions and methods that return iterables (including generators). For example, via the suffix <code>Iter</code>.</p>
  1608. <h3 id="conclusion">Conclusion</h3><p>I hope that this blog post convinced you that generators are a useful and versatile tool.</p>
  1609. <p>I like that generators let you implement cooperatively multitasked tasks that block while making asynchronous function calls. In my opinion that’s the right mental model for async calls. I hope that JavaScript goes further in this direction in the future. If one generator async-calls another generator, the indirection via promises is not needed. It could be avoided by <a href="https://github.com/lukehoban/ecmascript-asyncawait">the generator-based async functions</a> that have been proposed for ECMAScript 2016.</p>
  1610. <h3 id="references">References</h3><p>Acknowledgement: items 1–3 are sources of this blog post.</p>
  1611. <ol class="reflist">
  1612. <li>“<a href="https://github.com/jhusain/asyncgenerator">Async Generator Proposal</a>” by Jafar Husain</li>
  1613. <li>“<a href="http://www.dabeaz.com/coroutines/">A Curious Course on Coroutines and Concurrency</a>” by David Beazley</li>
  1614. <li>“<a href="http://calculist.org/blog/2011/12/14/why-coroutines-wont-work-on-the-web/">Why coroutines won't work on the web</a>” by David Herman</li>
  1615. <li>“<a href="http://exploringjs.com/">Exploring ES6: Upgrade to the next version of JavaScript</a>”, book by Axel</li>
  1616. <li><a href="http://www.2ality.com/2015/02/es6-iteration.html">Iterables and iterators in ECMAScript 6</a></li>
  1617. <li><a href="http://www.2ality.com/2015/01/es6-destructuring.html">Destructuring and parameter handling in ECMAScript 6</a></li>
  1618. <li>”<a href="the_spread_operator_%28...%29">The spread operator (<code>...</code>)</a>”, a section in the blog post “Destructuring and parameter handling in ECMAScript 6”.</li>
  1619. <li><a href="http://www.2ality.com/2015/03/babel-on-node.html">Using the ES6 transpiler Babel on Node.js</a></li>
  1620. <li><a href="http://www.2ality.com/2014/09/es6-promises-foundations.html">ECMAScript 6 promises (1/2): foundations</a> [explains the event loop and more]</li>
  1621. <li><a href="http://www.2ality.com/2014/10/es6-promises-api.html">ECMAScript 6 promises (2/2): the API</a></li>
  1622. <li><a href="http://www.2ality.com/2015/02/es6-classes-final.html">Classes in ECMAScript 6 (final semantics)</a></li>
  1623. </ol>