This code works as expected:
function sleep(ms) {
    return new Promise(resolve => setTimeout(resolve, ms));
}
async function getAsyncData() {
    await sleep(1000);  // simulate database/network delay...
    return [1, 2, 3, 4, 5];  // ...then return some data
}
const asyncIterable = (async function* filterAsyncData() {
    const items = await getAsyncData();
    for (const item of items) {
        yield item;
    }
})();
const asyncIterable2 = {
    [Symbol.asyncIterator]() {
        return {
            values: null,
            idx: 0,
            async next() {
                if (this.values === null) {
                    this.values = await getAsyncData();
                }
                if (this.idx < this.values.length) {
                    this.idx = this.idx + 1;
                    return Promise.resolve({ value: this.values[this.idx - 1], done: false });
                }
                return Promise.resolve({ done: true });
            }
        };
    }
};
async function main() {
    for await (const filteredItem of asyncIterable) {
        console.log(filteredItem);
    }
}
main()
It does not mather if I use asyncIterable or asyncIterable2 in the main function, I always get the same result. What is the best practice to define my iterable? Are there any guidelines about which option is preferred? Why?
 
    