Skip to content

Instantly share code, notes, and snippets.

@laser
Last active December 29, 2015 07:09
Show Gist options
  • Save laser/7634190 to your computer and use it in GitHub Desktop.
Save laser/7634190 to your computer and use it in GitHub Desktop.
Error-handling w/ES6 generators
function procrastinatingAdd(x, y) {
var errMsg = "Expected number and got ";
setTimeout(function() {
if (isNaN(x)) gen.throw(new TypeError(errMsg + typeof x));
if (isNaN(y)) gen.throw(new TypeError(errMsg + typeof y));
gen.next(x + y);
}, 500);
}
var gen = function* () {
try {
var result = yield procrastinatingAdd(1, "foo");
log(result);
}
catch (e) {
log(e);
}
};
gen = gen(); // activate the generator
gen.next(); // run it...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment