Skip to content

Instantly share code, notes, and snippets.

@tyage
Last active August 29, 2015 14:07
Show Gist options
  • Save tyage/eefcc06a8e7addf25b35 to your computer and use it in GitHub Desktop.
Save tyage/eefcc06a8e7addf25b35 to your computer and use it in GitHub Desktop.
// normal generator
function* gen() {
yield 2;
yield 3;
}
gen2 = function* () {}
a = gen()
for (b of a) {
console.log(b)
}
// generator method
obj = {
* a(){
yield 1;
yield 2;
}
};
a = obj.a()
for (b of a) {
console.log(b)
}
// yield can iterate
a = function*() {
var array = [1,2,3]
yield* array
yield* array
yield* array
}()
for (b of a) {
console.log(b)
}
// stop, throw (throw can be used in firefox34)
a = function*() {
var array = [1,2,3]
yield* array
yield* array
yield* array
}()
console.log(a.next())
a.throw(new Error('stop it'))
console.log(a.next())
// generator instance does not expose access to its generator function
console.log((new function *() {yield 1}).constructor) // => GeneratorFunctionPrototype
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment