Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Javascript generator, simple example
// Generator can be helpful for linearizing async logic.
// It allows you to "pause" code execution with "yield"
// and run to the next "yield" or end of generator with "next"
function* helloGen() {
// 1: 2:
var str = yield ' bro';
// 3:
console.log(str);
}
// create iterator
var it = helloGen();
// get ' bro' and pause (2)
var accost = it.next().value;
setTimeout(_ => {
// pass into iterator 'hello, bro', that
// will be assigned in str (1) and then
// will be printed out in the console (3)
it.next('hello,' + accost);
}, 5000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment