-
-
Save autoopsltd/3fd1c5dae43f288967bcc37a9a016e43 to your computer and use it in GitHub Desktop.
Revisions
-
learncodeacademy revised this gist
Aug 12, 2014 . 1 changed file with 26 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -32,11 +32,11 @@ console.log(gen.next(3)); //{value:undefined, done: true} So yippee, when do I ever have to yield numbers?<br/> At first generators seem somewhat useless<br/> The magic happens when smarter code wraps the generator<br/> Here's a pretty dumb version of a smart wrapper (full code at bottom of gist) ```javascript function smartCode(generator) { //give me a generator function var gen = generator();//start up the generator var yieldedVal = gen.next().value;//get the first yielded value if(yieldedVal.then) { //is it a promise? //it's a promise!!! yieldedVal.then(gen.next);//wait for it to resolve, then pass the resolved value back in @@ -87,4 +87,28 @@ Promise.coroutine(function* () { }; console.log(data.tweets, data.profile); })(); ``` **Here's that full code of a generator wrapper ```javascript function smartCode(generator) { return function() { var gen = generator.apply(this,arguments); function handleNext(yielded) { if (yielded.done) return yielded.value; //return final return value if (yielded.value.then) { return yielded.value.then(function(res) { return handleNext(gen.next(res)); }, function(err) { return handleNext(gen.throw(err)); }); } else { return handleNext(gen.next(yielded.value)); } } } } ``` -
learncodeacademy revised this gist
Aug 7, 2014 . 1 changed file with 16 additions and 8 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -45,7 +45,7 @@ function smartCode(generator) { //give me a generator function ``` So, let's use a library with the smarts, like Bluebird, Co, Q ```javascript //Bluebird Promise.coroutine(function* () { var tweets = yield $.get('tweets.json'); @@ -64,19 +64,27 @@ Promise.coroutine(function* () { ``` AWESOME! If you want to run them at the same time, yield an object or an array. ```javascript //Bluebird needs a little pre-config to yield arrays, //add this setup codesomewhere in your app Promise.coroutine.addYieldHandler(function(yieldedValue) { if (Array.isArray(yieldedValue)) return Promise.all(yieldedValue); }); Promise.coroutine(function* () { var [tweets, profile] = yield [$.get('tweets.json'), yield $.get('profile.json')]; console.log(tweets, profile); })(); //or set it up to yield an object and run this Promise.coroutine(function* () { var data = yield { tweets: $.get('tweets.json'), profile: yield $.get('profile.json') }; console.log(data.tweets, data.profile); })(); ``` -
learncodeacademy revised this gist
Aug 7, 2014 . 1 changed file with 3 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -29,8 +29,9 @@ console.log(gen.next(1)); //{value:2, done: false} console.log(gen.next(2)); //{value:3, done: false} console.log(gen.next(3)); //{value:undefined, done: true} ``` So yippee, when do I ever have to yield numbers?<br/> At first generators seem somewhat useless<br/> The magic happens when smarter code wraps the generator<br/> Here's a pretty dumb version of a smart wrapper ```javascript function smartCode(generator) { //give me a generator function -
learncodeacademy revised this gist
Aug 7, 2014 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -21,8 +21,8 @@ console.log(gen.next()); //{value:undefined, done: true} console.log(gen.next()); //errors because you can't call next() on a closed generator ``` The only problem here is that the final result it will log will be `undefined, undefined, undefined` Since yield sits in-between the yielded value and the rest of the function, we have to pass a value back in for it to get assigned to the variable. ```javascript console.log(gen.next()); //{value:1, done: false} console.log(gen.next(1)); //{value:2, done: false} -
learncodeacademy revised this gist
Aug 7, 2014 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -20,7 +20,7 @@ console.log(gen.next()); //{value:3, done: false} console.log(gen.next()); //{value:undefined, done: true} console.log(gen.next()); //errors because you can't call next() on a closed generator ``` The only problem here is that the final result it will log will be `undefined, undefined, undefined` Since yield sits in-between the yielded value and the rest of the function, we have to pass the new yielded value back in for it to get assigned to the variable. ```javascript -
learncodeacademy revised this gist
Aug 7, 2014 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,7 @@ ##what are generators## - They're pausable functions, pausable iterable functions, to be more precise - They're defined with the * - every time you `yield` a value, the function pauses until `.next(modifiedYieldValue)` is called ```javascript var myGen = function*() { var one = yield 1; -
learncodeacademy revised this gist
Aug 7, 2014 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,5 +1,5 @@ ##what are generators## - They're pausable functions, pausable iterable functions, to be more precise - They're defined with the * ```javascript var myGen = function*() { -
learncodeacademy revised this gist
Aug 7, 2014 . 1 changed file with 36 additions and 21 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,56 +1,70 @@ ##what are generators## - They're pausable functions, pausable iterable objects, to be more precise - They're defined with the * ```javascript var myGen = function*() { var one = yield 1; var two = yield 2; var three = yield 3; console.log(one, two, three); }; ``` Define the generator and now run it ```javascript var gen = myGen(); //get the generator ready to run //when you run next() on a generator, it runs until a yield, then waits until next() is called again console.log(gen.next()); //{value:1, done: false} console.log(gen.next()); //{value:2, done: false} console.log(gen.next()); //{value:3, done: false} console.log(gen.next()); //{value:undefined, done: true} console.log(gen.next()); //errors because you can't call next() on a closed generator ``` The only problem here is that the final result it will log will be undefined, undefined, undefined Since yield sits in-between the yielded value and the rest of the function, we have to pass the new yielded value back in for it to get assigned to the variable. ```javascript console.log(gen.next()); //{value:1, done: false} console.log(gen.next(1)); //{value:2, done: false} console.log(gen.next(2)); //{value:3, done: false} console.log(gen.next(3)); //{value:undefined, done: true} ``` So yippee, when do I ever have to yield numbers? Seems silly The magic happens when smarter code wraps the generator Here's a pretty dumb version of a smart wrapper ```javascript function smartCode(generator) { //give me a generator function var gen = generator();//start up the generator var yieldedVal = gen.next();//get the first yielded value if(yieldedVal.then) { //is it a promise? //it's a promise!!! yieldedVal.then(gen.next);//wait for it to resolve, then pass the resolved value back in } } ``` So, let's use a library with the smarts, like Bluebird, Co, Q ``` //Bluebird Promise.coroutine(function* () { var tweets = yield $.get('tweets.json'); console.log(tweets); })(); //Bluebird runs the generator, notices yield is a promise //so it waits on that promise, then passes it's value back to the generator when complete //here, it runs them in sequence, waiting for each to complete before proceeding Promise.coroutine(function* () { var tweets = yield $.get('tweets.json'); var profile = yield $.get('profile.json'); var friends = yield $.get('friends.json'); console.log(tweets, profile, friends); })(); ``` AWESOME! If you want to run them at the same time, yield an object or an array ```javascript //Bluebird needs a little pre-config to yield arrays, FYI Promise.coroutine(function* () { var data = yield { tweets: $.get('tweets.json'), @@ -63,3 +77,4 @@ Promise.coroutine(function* () { var [tweets, profile] = yield [$.get('tweets.json'), yield $.get('profile.json')]; console.log(tweets, profile); })(); ``` -
learncodeacademy created this gist
Aug 7, 2014 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,65 @@ //what are generators? //they're pausable functions, pausable iterable objects, to be more precise //they're defined with the * var myGen = function*() { var one = yield 1; var two = yield 2; var three = yield 3; console.log(one, two, three); }; var gen = myGen(); //get the generator ready to run //when you run next() on a generator, it runs until a yield, then waits until next() is called again console.log(gen.next()); //{value:1, done: false} console.log(gen.next()); //{value:2, done: false} console.log(gen.next()); //{value:3, done: false} console.log(gen.next()); //{value:undefined, done: true} console.log(gen.next()); //errors because you can't call next() on a closed generator //so yippee, when do I ever have to yield numbers? Seems silly //the magic happens when smarter code wraps the generator function smartCode(generator) { var gen = generator(); var yieldedVal = gen.next(); if(yieldedVal.then) { //it's a promise!!! yieldedVal.then(gen.next); } } //enter libraries like Co, Bluebird, Q...let's use Bluebird Promise.coroutine(function* () { var tweets = yield $.get('tweets.json'); console.log(tweets); })(); //Bluebird runs the generator, notices yield is a promise //so it waits on that promise, then passes it's value back to the generator when complete //here, it runs them in sequence, waiting for each to complete before proceeding Promise.coroutine(function* () { var tweets = yield $.get('tweets.json'); var profile = yield $.get('profile.json'); var friends = yield $.get('friends.json'); console.log(tweets, profile, friends); })(); //if you want to run them at the same time, yield an object or an array Promise.coroutine(function* () { var data = yield { tweets: $.get('tweets.json'), profile: yield $.get('profile.json') }; console.log(data.tweets, data.profile); })(); Promise.coroutine(function* () { var [tweets, profile] = yield [$.get('tweets.json'), yield $.get('profile.json')]; console.log(tweets, profile); })();