I would like to know if it is possible to implement an event with a maximum number of operations at a time. Example:
const myEvent = EventEmitter();
myEvent.on("calculate", (file) => {
// async operation here
// calls data event passing the data when finished. -> myEvent.emit("data", data);
});
arrayOfFiles.forEach((file), () => {
myEvent.emit("calculate", file);
});
myEvent.on("data", (data) => {
// do something here.
});
I want to allow only 500 files at a time. So I did this with the async.eachLimit
:
const myEvent = EventEmitter();
myEvent.on("calculate", (file) => {
// async operation here
// calls data event passing the data when finished. -> myEvent.emit("data", data);
});
async.eachLimit(arrayOfFiles, 500, (file, callback) => {
myEvent.emit("calculate", file);
myEvent.on("data", (data) => {
callback();
});
}, (error) => {
if (error) return console.error(error);
console.log("done!");
});
Which works fine, but maybe there is a better way to do this without using the async.eachLimit
.