Skip to content

Instantly share code, notes, and snippets.

@nathansmith
Last active July 1, 2022 15:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nathansmith/585ef92efc579914492e1c03798a5885 to your computer and use it in GitHub Desktop.
Save nathansmith/585ef92efc579914492e1c03798a5885 to your computer and use it in GitHub Desktop.
Silly JS generator example
/*
Code snippet from this tweet.
https://twitter.com/nathansmith/status/1542887226803101697
*/
// Generator function.
function* dealWithIt() {
// Step 1.
yield '🙂';
// Step 2.
return '😎';
}
// Get instance.
const emoji = dealWithIt();
// {value: '🙂', done: false}
console.log(emoji.next());
// {value: '😎', done: true}
console.log(emoji.next());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment