It generally creates an "iterator" so you can yield result's one at a time.
Similar to C#'s yield key work.
Official Information
Example
The “infinite” sequence of Fibonacci numbers (notwithstanding behavior around 2^53):
function* fibonacci() {
let [prev, curr] = [0, 1];
for (;;) {
[prev, curr] = [curr, prev + curr];
yield curr;
}
}
Generators can be iterated over in loops:
for (n of fibonacci()) {
// truncate the sequence at 1000
if (n > 1000)
break;
print(n);
}
Generators are iterators:
let seq = fibonacci();
print(seq.next()); // 1
print(seq.next()); // 2
print(seq.next()); // 3
print(seq.next()); // 5
print(seq.next()); // 8