Skip to content

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

Notifications You must be signed in to change notification settings

sadanandpai/generators-in-action

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 

Repository files navigation

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

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

About

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

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published