Last active
February 7, 2018 19:39
-
-
Save lorenmh/24f4731fc17a2d27bc8a10d0538c216c to your computer and use it in GitHub Desktop.
This file contains 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 characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment