| 1 |
<!DOCTYPE html> |
| 2 |
<html> |
| 3 |
<head profile="http://www.w3.org/2005/10/profile"> |
| 4 |
<title>Regenerator</title> |
| 5 |
<script src="codemirror/lib/codemirror.js"></script> |
| 6 |
<link rel="stylesheet" href="codemirror/lib/codemirror.css" /> |
| 7 |
<link rel="stylesheet" href="sandbox.css" /> |
| 8 |
<link rel="icon" type="image/png" href="yield_ahead.16px.png" /> |
| 9 |
<meta property="og:title" content="Regenerator" /> |
| 10 |
<meta property="og:type" content="website" /> |
| 11 |
<meta property="og:image" content="http://facebook.github.io/regenerator/yield_ahead.200px.png" /> |
| 12 |
<meta property="og:url" content="http://facebook.github.io/regenerator/" /> |
| 13 |
<script src="promise-6.0.0.js"></script> |
| 14 |
<script src="codemirror/mode/javascript.js"></script> |
| 15 |
<script src="bundle.js"></script> |
| 16 |
</head> |
| 17 |
<body> |
| 18 |
<div id="fb-root"></div> |
| 19 |
<script>(function(d, s, id) { |
| 20 |
var js, fjs = d.getElementsByTagName(s)[0]; |
| 21 |
if (d.getElementById(id)) return; |
| 22 |
js = d.createElement(s); js.id = id; |
| 23 |
js.src = "//connect.facebook.net/en_US/all.js#xfbml=1&appId=35921810593"; |
| 24 |
fjs.parentNode.insertBefore(js, fjs); |
| 25 |
}(document, 'script', 'facebook-jssdk')); |
| 26 |
</script> |
| 27 |
|
| 28 |
<a href="https://github.com/facebook/regenerator"> |
| 29 |
<img style="position: absolute; top: 0; right: 0; border: 0;" |
| 30 |
src="https://s3.amazonaws.com/github/ribbons/forkme_right_gray_6d6d6d.png" |
| 31 |
alt="Fork me on GitHub" /> |
| 32 |
</a> |
| 33 |
|
| 34 |
<div id="content"> |
| 35 |
<h1>Let’s get serious about ES6 generator functions.</h1> |
| 36 |
|
| 37 |
<div class="fb-like" |
| 38 |
data-href="https://facebook.github.io/regenerator" |
| 39 |
data-colorscheme="light" |
| 40 |
data-layout="standard" |
| 41 |
data-action="like" |
| 42 |
data-show-faces="true" |
| 43 |
data-send="true"> |
| 44 |
</div> |
| 45 |
|
| 46 |
<p> |
| 47 |
Several months have passed since support |
| 48 |
for <a href="http://jlongster.com/2012/10/05/javascript-yield.html">generator |
| 49 |
functions</a> and the <code>yield</code> |
| 50 |
keyword <a href="http://wingolog.org/archives/2013/05/08/generators-in-v8">arrived</a> |
| 51 |
in Node.js v0.11.2. This news was |
| 52 |
<a href="http://devsmash.com/blog/whats-the-big-deal-with-generators">greeted</a> |
| 53 |
<a href="http://tobyho.com/2013/06/16/what-are-generators/">with</a> |
| 54 |
<a href="http://blog.alexmaccaw.com/how-yield-will-transform-node">great</a> |
| 55 |
<a href="http://almostobsolete.net/coffeescript-generators.html">excitement</a>, |
| 56 |
because generator syntax |
| 57 |
provides <a href="http://jlongster.com/A-Study-on-Solving-Callbacks-with-JavaScript-Generators">a |
| 58 |
much cleaner alternative</a> to using callbacks when writing |
| 59 |
asynchronous server-side code. |
| 60 |
</p> |
| 61 |
|
| 62 |
<p> |
| 63 |
One of the biggest benefits of using JavaScript on the server is |
| 64 |
that you can (in theory at least) run the very same code in a web |
| 65 |
browser. However, if you choose to use generator functions in |
| 66 |
Node.js, you end up with a bunch of code that can't be executed |
| 67 |
client-side. So there's the rub: native support for generators is |
| 68 |
only so exciting because it enables you to write really clean, |
| 69 |
powerful, unportable code. |
| 70 |
</p> |
| 71 |
|
| 72 |
<p> |
| 73 |
Some of us on the JavaScript Infrastructure team at Facebook got |
| 74 |
restless waiting for the future to get here, so we developed a tool |
| 75 |
called <a href="https://npmjs.org/package/regenerator">regenerator</a> |
| 76 |
to replace generator functions with efficient JavaScript-of-today |
| 77 |
(ECMAScript 5 or ES5 for short) that behaves the same way. Since |
| 78 |
the tool itself is implemented in ES5, you can try it right now, |
| 79 |
in this web browser, without leaving this web page. |
| 80 |
</p> |
| 81 |
|
| 82 |
<p> |
| 83 |
Regenerator relies heavily on |
| 84 |
the <a href="https://github.com/ariya/esprima/tree/harmony">Esprima</a> |
| 85 |
JavaScript parser and two libraries that we maintain for |
| 86 |
manipulating abstract syntax |
| 87 |
trees, <a href="https://npmjs.org/package/ast-types">ast-types</a> |
| 88 |
and <a href="https://npmjs.org/package/recast">recast</a>. It is |
| 89 |
similar in spirit to |
| 90 |
Google's <a href="https://github.com/google/traceur-compiler">Traceur |
| 91 |
Compiler</a>, which supports generators and many other ES6 |
| 92 |
features through source transformation, but we would argue it |
| 93 |
compares favorably to |
| 94 |
Traceur <a href="javascript:toggleComparison()">in several |
| 95 |
ways</a><span id="punctuation">.</span> |
| 96 |
<ul id="comparison" class="hidden"> |
| 97 |
<li> |
| 98 |
Traceur supports <code>yield</code> expressions only on the |
| 99 |
right-hand sides of assignment statements and variable |
| 100 |
declarations, or as standalone statements, whereas regenerator |
| 101 |
allows a <code>yield</code> expression to appear anywhere an |
| 102 |
expression is permitted to appear. |
| 103 |
</li> |
| 104 |
<li> |
| 105 |
Regenerator aims to generate as little boilerplate as |
| 106 |
possible, whereas Traceur |
| 107 |
generates <a href="http://google.github.io/traceur-compiler/demo/repl.html#function%20*gen(x)%20%7B%0A%20%20return%20yield%20x%3B%0A%7D" |
| 108 |
target="_blank">twice as much code</a> for the simplest of generators. |
| 109 |
</li> |
| 110 |
<li> |
| 111 |
Regenerator transforms generator functions and nothing else, |
| 112 |
so you don't have to buy into the entire Traceur runtime just |
| 113 |
to get support for generators. |
| 114 |
</li> |
| 115 |
</ul> |
| 116 |
</p> |
| 117 |
|
| 118 |
<p> |
| 119 |
Please give the transformer a try below, and feel free to report |
| 120 |
bugs. Regenerator |
| 121 |
is <a href="https://github.com/facebook/regenerator/blob/master/test/tests.es6.js">well-tested</a> |
| 122 |
and feature-complete, but we'd love your help in making it |
| 123 |
completely bulletproof! |
| 124 |
</p> |
| 125 |
|
| 126 |
<div id="sandbox" class="clearfix"> |
| 127 |
<div id="inputWrapper"> |
| 128 |
<h2 class="clearfix">Input: |
| 129 |
<span class="hint"> |
| 130 |
<a href="javascript:reportBug()">report</a> a bug |
| 131 |
</span> |
| 132 |
</h2> |
| 133 |
</div> |
| 134 |
<div id="outputWrapper"> |
| 135 |
<h2 class="clearfix">Output: |
| 136 |
<span class="hint"> |
| 137 |
control-return to <a href="javascript:evaluateOutput()">run</a> |
| 138 |
</span> |
| 139 |
</h2> |
| 140 |
</div> |
| 141 |
</div> |
| 142 |
</div> |
| 143 |
|
| 144 |
<span id="attribution">An open-source project from Facebook.</span> |
| 145 |
<span id="copyright">© 2013 Facebook Inc.</span> |
| 146 |
|
| 147 |
<script src="sandbox.js"></script> |
| 148 |
</body> |
| 149 |
</html> |