sindresorhus / p-queue

Promise queue with concurrency control
MIT License
3.45k stars 185 forks source link

Adding items to current queue? #85

Open kevzz1994 opened 5 years ago

kevzz1994 commented 5 years ago

I am currently using p-queue in combination with Puppeteer. What I want to achieve is to add the current function to the queue if an exception gets thrown. I am running with concurrency set to 3.

I have the following snippets:

getAccounts is simply a method for parsing a json file.

    async init() {
        let accounts = await this.getAccounts();
        accounts.map(async () => {
            await queue.add(() => this.test());
        });
        await queue.onIdle();
        console.log("Done");
    }
async test() {
        const browser = await puppeteer.launch({headless: false});
        try {
            const page = await browser.newPage();
            await page.goto(this._url);

            if (Math.floor(Math.random() * 10) > 4) {
                throw new Error("Simulate error");
            }

            await browser.close();
        } catch (error) {
            await browser.close();
            await queue.add(() => this.test());
            console.log(error);
        }
    }

When it goes into the catch, the queue just stops. I am not sure if my implementation is right. But it also does not execute console.log("Done");.

sindresorhus commented 4 years ago

That looks correct to me. I would try to use add breakpoints and inspect the state at the point it blocks to try to track down why.

I can't really do much more here unless you submit a failing test.