function* fibonacci() {
let current = 1;
let next = 1;
while (true) {
yield current;
[current, next] = [next, current + next];
}
}
const seq = fibonacci().take(3);
console.log(seq.next().value); // 1
console.log(seq.next().value); // 1
console.log(seq.next().value); // 2
console.log(seq.next().value); // undefinedIterator.prototype.take() / notebook
Iterator.prototype.take()
Runnable notebook: every JavaScript, HTML and CSS example on this page executes in a sandboxed local frame. Nothing leaves the browser.
for (const n of fibonacci().take(5)) {
console.log(n);
}
// Logs:
// 1
// 1
// 2
// 3
// 5for (const n of fibonacci().drop(2).take(5)) {
// Drops the first two elements, then takes the next five
console.log(n);
}
// Logs:
// 2
// 3
// 5
// 8
// 13
for (const n of fibonacci().take(5).drop(2)) {
// Takes the first five elements, then drops the first two
console.log(n);
}
// Logs:
// 2
// 3
// 5fibonacci().take(-1); // RangeError: -1 must be positive
fibonacci().take(undefined); // RangeError: undefined must be positivefor (const n of new Set([1, 2, 3]).values().take(Infinity)) {
console.log(n);
}
// Logs:
// 1
// 2
// 3