Skip to content

Instantly share code, notes, and snippets.

@savelee
Last active October 7, 2016 08:04
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 savelee/8adac424eec58313d2a9ae0fc9cc9e4f to your computer and use it in GitHub Desktop.
Save savelee/8adac424eec58313d2a9ae0fc9cc9e4f to your computer and use it in GitHub Desktop.
function* dice () {
while (true) {
try {
yield Math.floor(Math.random() * 6) + 1;
} catch(e) {
console.log("Error caught!");
}
}
}
var d = dice();
d.next(); //Object { value: 3, done: false }
d.next(); //Object { value: 6, done: false }
d.throw(new Error("Something went wrong"));
//Error caught!
//Object { value: 6, done: false }
function* generator() {
yield "image1.png";
// pause
yield "image2.png";
// pause
yield "image3.png";
// done
}
var slides = generator();
slides.next(); //{ value: "image1.png", done: false }
slides.next(); //{ value: "image2.png", done: false }
slides.next(); //{ value: "image3.png", done: false }
slides.next(); //{ value: undefined, done: true }
for (let val of slideshow()) {
console.log(val); //image1.png
//image2.png
//image3.png
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment