Skip to content

Instantly share code, notes, and snippets.

@btg5679
Created July 11, 2018 01:30
Show Gist options
  • Save btg5679/ffe53b3522abeb49ac11ea9844890899 to your computer and use it in GitHub Desktop.
Save btg5679/ffe53b3522abeb49ac11ea9844890899 to your computer and use it in GitHub Desktop.
function* favBeer() {
const reply = yield "What is your favorite type of beer?";
console.log(reply);
if (reply !== "ipa") return "No soup for you!";
return "OK, soup.";
}
{
const it = favBeer();
const q = it.next().value; // Iterator asks question
console.log(q);
const a = it.next("lager").value; // Question is answered
console.log(a);
}
// What is your favorite beer?
// lager
// No soup for you!
{
const it = favBeer();
const q = it.next().value; // Iterator asks question
console.log(q);
const a = it.next("ipa").value; // Question is answered
console.log(a);
}
// What is your favorite been?
// ipa
// OK, soup.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment