Skip to content

Instantly share code, notes, and snippets.

@autoopsltd
Forked from learncodeacademy/generators.md
Created March 29, 2018 15:11
Show Gist options
  • Save autoopsltd/3fd1c5dae43f288967bcc37a9a016e43 to your computer and use it in GitHub Desktop.
Save autoopsltd/3fd1c5dae43f288967bcc37a9a016e43 to your computer and use it in GitHub Desktop.

Revisions

  1. @learncodeacademy learncodeacademy revised this gist Aug 12, 2014. 1 changed file with 26 additions and 2 deletions.
    28 changes: 26 additions & 2 deletions generators.md
    Original 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
    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();//get the first yielded value
    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));
    }
    }
    }
    }
    ```
  2. @learncodeacademy learncodeacademy revised this gist Aug 7, 2014. 1 changed file with 16 additions and 8 deletions.
    24 changes: 16 additions & 8 deletions generators.md
    Original 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
    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
    //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);
    })();

    Promise.coroutine(function* () {
    var [tweets, profile] = yield [$.get('tweets.json'), yield $.get('profile.json')];
    console.log(tweets, profile);
    })();
    ```
  3. @learncodeacademy learncodeacademy revised this gist Aug 7, 2014. 1 changed file with 3 additions and 2 deletions.
    5 changes: 3 additions & 2 deletions generators.md
    Original 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? Seems silly
    The magic happens when smarter code wraps the generator
    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
  4. @learncodeacademy learncodeacademy revised this gist Aug 7, 2014. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions generators.md
    Original 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
    the new yielded value back in for it to get assigned to the variable.
    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}
  5. @learncodeacademy learncodeacademy revised this gist Aug 7, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion generators.md
    Original 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
    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
  6. @learncodeacademy learncodeacademy revised this gist Aug 7, 2014. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions generators.md
    Original 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;
  7. @learncodeacademy learncodeacademy revised this gist Aug 7, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion generators.md
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    ##what are generators##
    - They're pausable functions, pausable iterable objects, to be more precise
    - They're pausable functions, pausable iterable functions, to be more precise
    - They're defined with the *
    ```javascript
    var myGen = function*() {
  8. @learncodeacademy learncodeacademy revised this gist Aug 7, 2014. 1 changed file with 36 additions and 21 deletions.
    57 changes: 36 additions & 21 deletions generators.md
    Original 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 *
    ##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

    //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) {
    ```
    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);
    yieldedVal.then(gen.next);//wait for it to resolve, then pass the resolved value back in
    }
    }
    ```

    //enter libraries like Co, Bluebird, Q...let's use Bluebird
    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);
    })();
    ```




    //if you want to run them at the same time, yield an object or an array
    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);
    })();
    ```
  9. @learncodeacademy learncodeacademy created this gist Aug 7, 2014.
    65 changes: 65 additions & 0 deletions generators.md
    Original 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);
    })();