Last active
October 7, 2016 08:04
-
-
Save savelee/8adac424eec58313d2a9ae0fc9cc9e4f to your computer and use it in GitHub Desktop.
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* 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 } |
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* 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