Can an async
generator be somehow broadcast or multicast, so that all its iterators ("consumers"? subscribers?) receive all values?
Consider this example:
const fetchMock = () => "Example. Imagine real fetch";
async function* gen() {
for (let i = 1; i <= 6; i++) {
const res = await fetchMock();
yield res.slice(0, 2) + i;
}
}
const ait = gen();
(async() => {
// first "consumer"
for await (const e of ait) console.log('e', e);
})();
(async() => {
// second...
for await (const é of ait) console.log('é', é);
})();
Iterations "consume" a value, so only one or the other gets it.
I would like for both of them (and any later ones) to get every yield
ed value, if such a generator is possible to create somehow. (Similar to an Observable
.)
gen
accepts callback functions that are applied to each yielded thing? Is there a specific reason you want / need to use generators? – Kineticsgen
could accept a callback. I'm using async generators because they are a built-in feature of JS, otherwise I would use anObservable
for this but I thought they do almost the same thing (except for the ability to "multicast") – Cousin