Ron Buckton (2015-03-24T22:35:51.000Z)
d at domenic.me (2015-04-14T22:14:11.635Z)
Is your goal to wrap a generator, as it seems you are propagating the exception of the caller by calling iterator.throw(). However, you do not seem to be propagating the sent value, so the protocol here isn’t fully implmeneted. If you just want to iterate values (and don’t really care about the return value of the iterable or propagating a thrown exception, you could write: ```js function* take(n, iterable) { n |= 0; if (n <= 0) { return; } // let for..of call return() for (let value of iterable) { yield value; if (n-- <= 0) { return; } } } ``` If you want to support the full communication channel of a generator, you could write: ```js function* take(n, iterable) { let iterator = iterable[Symbol.iterator](); let step = () => iterator.next(); n |= 0; // try..finally outside of loop try { let sent; while (n > 0) { let { value, done } = step(); if (done) { return value; } n--; // try..catch local to the yield try { sent = yield value; step = () => iterator.next(sent); } catch (e) { if (typeof iterator.throw === "function") { step = () => iterator.throw(e); } else { throw e; } } } } finally { if (typeof iterator.return === "function") { iterator.return(); } } } ```