This article's content
Async Generator in TypeScript
type Person = {
    name: string;
    title: string;
}

let items: Person[] = [
    {
        name: "Bing Bong",
        title: "The Archer"
    },
    {
        name: "Hector",
        title: "The well endowed"
    },
    {
        name: "Brutalitops",
        title: "The Magician"
    }
];

async function* getItems(): AsyncGenerator<Person> {
    for (let item of items) {
        await new Promise(r => setTimeout(r, 4000));
        yield item;
    }
}

async function renderOverTime() {
    for await (let item of getItems()) {
        console.log(`${item.name} ${new Date().toLocaleTimeString()}`);
    }
}

renderOverTime();

About Author

Mathias Bothe To my job profile

I am Mathias, born 40 years ago in Heidelberg, Germany. Today I am living in Munich and Stockholm. I am a passionate IT freelancer with more than 16 years experience in programming, especially in developing web based applications for companies that range from small startups to the big players out there. I am founder of bosy.com, creator of the security service platform BosyProtect© and initiator of several other software projects.