Upload
venom087
View
235
Download
0
Embed Size (px)
Citation preview
8/11/2019 Diving Deeper With ES6 Generators
1/12
2.9.2014 Diving Deeper With ES6 Generators
http://davidwalsh.name/es6-generators-dive 1/12
Diving Deeper With ES6 Generators
By Kyle Simpson on July 27, 2014 162 25 10
The Basics Of ES6 GeneratorsDiving Deeper With ES6 GeneratorsGoing Async With ES6 GeneratorsGetting Concurrent With ES6 Generators
"Part 1: The Basics Of ES6 Generators"
function *foo () {
try {var = yield 3;
. log ( "x: " + ); // may never get here!}
http://dwf.tw/wufoohttp://davidwalsh.name/es6-generatorshttp://davidwalsh.name/concurrent-generatorshttp://davidwalsh.name/async-generatorshttp://davidwalsh.name/es6-generators-divehttp://davidwalsh.name/es6-generatorshttp://getify.me/8/11/2019 Diving Deeper With ES6 Generators
2/12
2.9.2014 Diving Deeper With ES6 Generators
http://davidwalsh.name/es6-generators-dive 2/12
catch ( ) {. log ( "Error: " + );
}}
var = foo ();
var = . next (); // { value:3, done:false }
// instead of resuming normally with another `next(..)` call,// let's throw a wrench (an error) into the gears:
. throw ( "Oops!" ); // Error: Oops!
function *foo () { }
var = foo ();try {
. throw ( "Oops!" );}catch ( ) {
. log ( "Error: " + ); // Error: Oops!}
8/11/2019 Diving Deeper With ES6 Generators
3/12
2.9.2014 Diving Deeper With ES6 Generators
http://davidwalsh.name/es6-generators-dive 3/12
function *foo () {var = yield 3;var = . toUpperCase (); // could be a TypeError error!
yield ;}
var = foo ();
. next (); // { value:3, done:false }
try {. next ( 42 ); // `42` won't have `toUpperCase()`
}catch ( ) {
. log ( ); // TypeError (from `toUpperCase()` call)}
function *foo () {yield 3;yield 4;
}
function *bar () {yield 1;yield 2;
yield *foo (); // `yield *` delegates iteration control to `foo()`yield 5;
}
8/11/2019 Diving Deeper With ES6 Generators
4/12
2.9.2014 Diving Deeper With ES6 Generators
http://davidwalsh.name/es6-generators-dive 4/12
for ( var bar ()) {. log ( );
}// 1 2 3 4 5
function *foo () {var = yield 3;var = yield 4;
. log ( "z: " + + ", w: " + );}
function *bar () {var = yield 1;var = yield 2;yield *foo (); // `yield*` delegates iteration control to `foo()`
8/11/2019 Diving Deeper With ES6 Generators
5/12
2.9.2014 Diving Deeper With ES6 Generators
http://davidwalsh.name/es6-generators-dive 5/12
var = yield 5;. log ( "x: " + + ", y: " + + ", v: " + );
}
var = bar ();
. next (); // { value:1, done:false }
. next ( "X" ); // { value:2, done:false }
. next ( "Y" ); // { value:3, done:false }
. next ( "Z" ); // { value:4, done:false }
. next ( "W" ); // { value:5, done:false }// z: Z, w: W
. next ( "V" ); // { value:undefined, done:true }// x: X, y: Y, v: V
function *foo () {yield 2;yield 3;return "foo" ; // return value back to `yield*` expression
}
function *bar () {yield 1;var = yield *foo ();
. log ( "v: " + );yield 4;
}
var = bar ();
. next (); // { value:1, done:false }
. next (); // { value:2, done:false }
. next (); // { value:3, done:false }
. next (); // "v: foo" { value:4, done:false }
. next (); // { value:undefined, done:true }
8/11/2019 Diving Deeper With ES6 Generators
6/12
8/11/2019 Diving Deeper With ES6 Generators
7/12
2.9.2014 Diving Deeper With ES6 Generators
http://davidwalsh.name/es6-generators-dive 7/12
. throw ( "Uh oh!" ); // will be caught inside `foo()`// foo caught: Uh oh!
. next (); // { value:undefined, done:true } --> No error here!// bar caught: Oops!
8/11/2019 Diving Deeper With ES6 Generators
8/12
2.9.2014 Diving Deeper With ES6 Generators
http://davidwalsh.name/es6-generators-dive 8/12
getify.me getify posts
Recent Features
CSS filters
Incredible Demos
five awesome Mozilla technologies that most people have neverheard of
http://davidwalsh.name/facebook-meta-tagshttp://davidwalsh.name/mozilla-technologieshttp://davidwalsh.name/togetherjshttp://davidwalsh.name/tutorials/demoshttp://davidwalsh.name/canvas-demoshttps://dvcs.w3.org/hg/FXTF/raw-file/tip/filters/index.htmlhttp://davidwalsh.name/css-filtershttp://davidwalsh.name/tutorials/featureshttp://dwf.tw/ydkjsallhttp://davidwalsh.name/author/kylesimpsonhttp://twitter.com/getifyhttp://getify.me/8/11/2019 Diving Deeper With ES6 Generators
9/12
2.9.2014 Diving Deeper With ES6 Generators
http://davidwalsh.name/es6-generators-dive 9/12
Discussion
yield *
function * gen (){yield * [ "a" , "b" , "c" ]
}
gen (). next () // { value:"a", done:false }
http://getify.me/http://webplatformdaily.org/http://getify.me/http://twitter.com/ziyunfeihttp://www.ifeenan.com/8/11/2019 Diving Deeper With ES6 Generators
10/12
2.9.2014 Diving Deeper With ES6 Generators
http://davidwalsh.name/es6-generators-dive 10/12
http://getify.me/http://atomictumato.blogspot.com/http://www.brandingly.org/http://atomictumato.blogspot.com/8/11/2019 Diving Deeper With ES6 Generators
11/12
2.9.2014 Diving Deeper With ES6 Generators
http://davidwalsh.name/es6-generators-dive 11/12
Name Email Website
Post Comment! Use Code Editor
8/11/2019 Diving Deeper With ES6 Generators
12/12
2.9.2014 Diving Deeper With ES6 Generators