https://github.com/sadanandpai/generators-in-action
Working of multiple generators with the help of queue data structure where the one generator yields to trigger the next one in chain till the completion of all
https://github.com/sadanandpai/generators-in-action
generator-functions javascript
Last synced: about 1 month ago
JSON representation
Working of multiple generators with the help of queue data structure where the one generator yields to trigger the next one in chain till the completion of all
- Host: GitHub
- URL: https://github.com/sadanandpai/generators-in-action
- Owner: sadanandpai
- Created: 2020-12-02T15:10:48.000Z (almost 5 years ago)
- Default Branch: main
- Last Pushed: 2021-02-15T06:41:58.000Z (over 4 years ago)
- Last Synced: 2025-09-02T00:35:51.209Z (about 1 month ago)
- Topics: generator-functions, javascript
- Language: JavaScript
- Homepage: https://sadanandpai.github.io/generators-in-action/
- Size: 14.6 KB
- Stars: 4
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Generators in action
Working of multiple generators with the help of queue data structure where one generator yields to trigger the next one in chain till the completion of all
## Core logic
```js
async function* gen(counter, progress) {
while (true) {
await new Promise((resolve) => setTimeout(resolve, 400));
if (status) {
yield;
}
}
}const iteratorQueue = [gen(), gen(), gen(), gen()];
async function startGenerators() {
while (iteratorQueue.length !== 0) {
const iterator = iteratorQueue.shift();
const value = await iterator.next();
if (!value.done) {
iteratorQueue.push(iterator);
}
status = false;
}
}// set status = true to yield the currently running generator
```