Skip to content

Instantly share code, notes, and snippets.

@ericelliott
Created May 20, 2016 07:14
Show Gist options
  • Save ericelliott/890c20d18bcc4362048dba2dca8e67ac to your computer and use it in GitHub Desktop.
Save ericelliott/890c20d18bcc4362048dba2dca8e67ac to your computer and use it in GitHub Desktop.

Revisions

  1. ericelliott created this gist May 20, 2016.
    31 changes: 31 additions & 0 deletions passing-values-to-generators.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,31 @@
    function* crossBridge() {
    const reply = yield 'What is your favorite color?';
    console.log(reply);
    if (reply !== 'yellow') return 'Wrong!'
    return 'You may pass.';
    }

    {
    const iter = crossBridge();
    const q = iter.next().value; // Iterator yields question
    console.log(q);
    const a = iter.next('blue').value; // Pass reply back into generator
    console.log(a);
    }

    // What is your favorite color?
    // blue
    // Wrong!


    {
    const iter = crossBridge();
    const q = iter.next().value;
    console.log(q);
    const a = iter.next('yellow').value;
    console.log(a);
    }

    // What is your favorite color?
    // yellow
    // You may pass.