Forked from ericelliott/passing-values-to-generators.js
Created
February 9, 2022 09:00
-
-
Save sauravdroid/534dd1c5c0f6934ae0cf0f37baff71ee to your computer and use it in GitHub Desktop.
Passing values into generators through `.next()`
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* 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. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment