An open API service indexing awesome lists of open source software.

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

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

Live Demo

## 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
```