Skip to content

Instantly share code, notes, and snippets.

@deepak
Last active August 29, 2015 14: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 deepak/f41c622efc574ebab7c1 to your computer and use it in GitHub Desktop.
Save deepak/f41c622efc574ebab7c1 to your computer and use it in GitHub Desktop.
cannot throw error in generator
works if generator is not calling another function ?
example from http://davidwalsh.name/es6-generators-dive
similar example at https://gist.github.com/deepak/9a7337d7a641f8af9c8b
function *foo() {
try {
var x = yield getThree();
console.log( "x: " + x ); // may never get here!
}
catch (err) {
console.log(err.toString());
}
}
function getThree() {
it.next(3);
}
var it = foo();
var res = it.next();
console.log(res);
it.throw("Oops!");
// output
// Error: Generator is already running
// {"done":true}
var it = foo();
function* foo() {
try {
var x = yield 3;
console.log( "x: " + x ); // may never get here!
}
catch (err) {
console.log( "Error: " + err );
}
}
var res = it.next();
console.log(res);
it.throw("Oops!");
// output
// {"value":3,"done":false}
// Error: Oops!
@deepak
Copy link
Author

deepak commented Feb 4, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment