So I was brushing up on some JS, and came across the following example for a simple Fibonacci calculator:
function* fibs() {
var a = 0;
var b = 1;
while (true) {
yield a;
[a, b] = [b, a + b];
}
}
var [first, second, third, fourth, fifth, sixth] = fibs();
// first = 0
// second = 1
// third = 1
// fourth = 2
// fifth = 3
// sixth = 5
Regardless of how many elements you put in the results array, the function loops precisely the right number of times, and gets out. Never more, never less, no errors.
var [first, second] = fibs(); // Works great!
var [a, b, c, d, e, f, g, h, I, j, k, l] = fibs(); // Still works!
I've been combing the docs, but can't find anything that might allude to what's happening.
How does the generator know when to break out of the loop and be done with it? There are no exit conditions, and nothing to suggest this would do anything but run for eternity.
Much thanks.
EDIT #1
Is there another way to use this same generator to get the nth number in the sequence, without a potentially huge results array?
How does the generator know when to break out of the loop and be done with it?
It doesn't. The generator never breaks out of the loop. It's just that the destructuring assignment only calls the generator's iterator X times (as many as the number of variables in the destructuring assignment). The iterator doesn't know you're done with it, it just responds to the "next" calls it gets.
It's a bit like this:
function* fibs() {
var a = 0;
var b = 1;
while (true) {
yield a;
[a, b] = [b, a + b];
}
}
// Pseudo-destructuring:
let first, second, third;
{
const iterator = fibs();
first = iterator.next().value;
second = iterator.next().value;
third = iterator.next().value;
// Now, our "destructuring" is done, so we just...don't call
// the iterator anymore
}
console.log(first, second, third);
It's not the generator, but the destructuring assignment that sets the limit.