Skip to content

Instantly share code, notes, and snippets.

@mrister
Created August 8, 2016 22:24
Show Gist options
  • Save mrister/0e9f422706eb3c38918696359a01143e to your computer and use it in GitHub Desktop.
Save mrister/0e9f422706eb3c38918696359a01143e to your computer and use it in GitHub Desktop.
Throwing an error to the generator function.
function *genTestError() {
while (true) {
try {
var val = yield null;
console.log('value', val);
} catch (e) {
console.log('There was an error', e);
}
}
}
var test = genTestError();
test.next();
// Invoke the error by throwing it
test.throw(new Error('test error'));
// logs There was an error Error: test error
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment