Created
February 3, 2015 12:16
-
-
Save deepak/9a7337d7a641f8af9c8b to your computer and use it in GitHub Desktop.
cannot throw error from ES6 generator
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 getStockPrice() { | |
seq.throw('oops! could not get stock price'); | |
// seq.next(80); | |
} | |
function executeTrade() { | |
setTimeout(function() { | |
console.log("trade done"); | |
seq.next(true); | |
}, 300); | |
} | |
var stockTrade = function* () { | |
try { | |
var price = yield getStockPrice(); | |
console.log('toots'); | |
if(price > 45) { | |
executeTrade(); | |
} else { | |
console.log('trade not done'); | |
} | |
} catch(ex) { | |
// always says: | |
// caught error: Generator is already running | |
// expected "oops! could not get stock price" | |
console.log("caught error: " + ex.message); | |
} | |
} | |
var seq = stockTrade(); | |
seq.next(); // {value: undefined, done: true} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
working example at https://gist.github.com/deepak/f0d2066bbedd51c3add3#file-wrap_errors_for_generators-es-js-L10