@mcjxy/task-pool
Version:
Easy way to manage a pool of workers, support cluster mode(run tasks in different process), thread mode(run tasks in different thread) and normal mode(run tasks in current thread)!
23 lines (20 loc) • 545 B
JavaScript
;
const fs = require('fs');
const path = require('path');
let count = 0;
module.exports = async (id, data) => {
return new Promise((resolve, reject) => {
setTimeout(() => {
try {
fs.writeFileSync(path.resolve(__dirname, data + '.txt'), 'worker ' + id + ' : data ' + data + ' count: ' + count++);
} catch (e) {
reject(e);
}
if (parseInt('0' + data) % 2 === 1) {
reject(new Error(data));
} else {
resolve(data);
}
}, 1000);
});
};