Skip to content

Instantly share code, notes, and snippets.

@lorenmh
Last active February 7, 2018 19:39
Show Gist options
  • Save lorenmh/24f4731fc17a2d27bc8a10d0538c216c to your computer and use it in GitHub Desktop.
Save lorenmh/24f4731fc17a2d27bc8a10d0538c216c to your computer and use it in GitHub Desktop.
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