Skip to content

Instantly share code, notes, and snippets.

@lorenmh
Last active February 7, 2018 19:39
Show Gist options
  • Select an option

  • Save lorenmh/24f4731fc17a2d27bc8a10d0538c216c to your computer and use it in GitHub Desktop.

Select an option

Save lorenmh/24f4731fc17a2d27bc8a10d0538c216c to your computer and use it in GitHub Desktop.

Revisions

  1. lorenmh created this gist Feb 7, 2018.
    40 changes: 40 additions & 0 deletions echoGenerator.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,40 @@
    function* echoGenerator() {
    let received = null;

    while (true) {
    received = yield received;

    if (received === null) return;

    received = 'ECHO ' + received;
    }
    }

    messages = ['hello', 'these', 'are', 'some', 'messages', null];

    let echo = echoGenerator();

    for (message of messages) {
    console.log(`Sending: ${message}`);
    let received = echo.next(message);

    if (received.done) {
    console.log('Done receiving');
    break;
    }

    console.log(`Received: ${received.value}`);
    }

    // Sending: hello
    // Received: null
    // Sending: these
    // Received: ECHO these
    // Sending: are
    // Received: ECHO are
    // Sending: some
    // Received: ECHO some
    // Sending: messages
    // Received: ECHO messages
    // Sending: null
    // Done receiving