Skip to content

Waiting for the queue to be fully processed #47

@robogeek

Description

@robogeek

Your documentation didn't help me understand how to use the drain function to wait until the queue is fully processed.

What I came up with is:

const queue = fastq.promise(renderDocumentInQueue,
                            config.concurrency);

const waitFor = [];
for (let entry of filez) {
    waitFor.push(queue.push(entry));
}

await Promise.all(waitFor);

This is using the Promise version of fastq. In that version, the push method returns a Promise. Therefore pushing the Promise's into an array lets us use Promise.all to know when all tasks have been completed.

Does this look correct?

An alternative I've coded but haven't tested is:

// await new Promise((resolve, reject) => {
//    queue.drain = function() {
//        resolve();
//    }
// });

It seems from your test scripts that one simply assigns a function to the drain field, and it'll automatically be called. Therefore one way to wait for this function to be called is this Promise here.

Is either preferable over the other?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions