Last active
December 29, 2015 07:09
-
-
Save laser/7634190 to your computer and use it in GitHub Desktop.
Error-handling w/ES6 generators
This file contains hidden or 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 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