@ct0r/worker-queue
v0.1.1
Published
Worker thread queue for Node.js
Downloads
3
Maintainers
Readme
ct0r/worker-queue
Worker thread queue for Node.js.
Installation
npm install @ct0r/worker-queue
Usage
// index.js
const queue = require("@ct0r/worker-queue");
const enqueue = queue({ parallelism: 2 });
Promise.all([
enqueue("./parse-xml-worker.js", xml1),
enqueue("./parse-xml-worker.js", xml2),
]);
// parse-xml-worker.js
const { work } = require("@ct0r/worker-queue");
work((xml) => {
// ...
});
or
const enqueue = queue({
filename: "./parse-xml-worker.js",
parallelism: 2,
});
await Promise.all([enqueue(xml1), enqueue(xml2)]);
// or
await Promise.all([xml1, xml].map(enqueue));
API
queue({ filename, parallelism = 1 })
Sets level of parallelism and returns enqueue
function.
enqueue(...args)
Returned by queue
if filename
is provided.
enqueue(filename, ...args)
Returned by queue
if filename
is not provided.
work(fn)
Invokes given function inside of worker and delegates errors back to queue.