Generator functions use yield to produce a sequence one value at a time.

Generator Functions

generator.ts
function* countUp(limit: number): Generator<number> {
    for (let current: number = 1; current <= limit; current++) {
        yield current;
    }
}

const limit: number = ;
const parts: string[] = [];

for (const value of countUp(limit)) {
    parts.push(String(value));
}

console.log(parts.join(","));
function* countUp(limit: number): Generator<number> {
    for (let current: number = 1; current <= limit; current++) {
        yield current;
    }
}

const limit: number = ;
const parts: string[] = [];

for (const value of countUp(limit)) {
    parts.push(String(value));
}

console.log(parts.join(","));
function* countUp(limit: number): Generator<number> {
    for (let current: number = 1; current <= limit; current++) {
        yield current;
    }
}

const limit: number = ;
const parts: string[] = [];

for (const value of countUp(limit)) {
    parts.push(String(value));
}

console.log(parts.join(","));
generator A generator function uses `function*` and `yield` to create an iterable sequence.