1function* fibonacci() { // a generator function
2 let [prev, curr] = [0, 1];
3 while (true) {
4 [prev, curr] = [curr, prev + curr];
5 yield curr;
6 }
7}
8
9for (const n of fibonacci()) {
10 console.log(n);
11 // truncate the sequence at 1000
12 if (n >= 1000) {
13 break;
14 }
15}
16