javascript - (Why) Can I not throw an exception out of a generator? -
i'm trying throw exception body of es6 generator function, it's not going through. part of es6 specification or quirk of babel?
here code tried (on babeljs.io):
function *gen() { throw new error('x'); } try { gen(); console.log('not throwing'); } catch(e) { console.log('throwing'); }
if indeed specified es6 behavior, what's alternative approach signalling exception?
you created iterator didn't run it.
var g = gen(); g.next(); // throws 'x'
here's example:
function *gen() { (let i=0; i<10; i++) { yield i; if (i >= 5) throw new error('x'); } } try { (n of gen()) console.log(n); // throw after `5` console.log('not throwing'); } catch(e) { console.log('throwing', e); }
Comments
Post a Comment