I want to implement a JS class that executes at most N tasks in parallel using JS promises or generators. Something like
class Executor {
constructor(numberOfMaxTasks) {
...
}
next(task) {
...
}
done(onDone) {
...
}
}
....
const executor = new Executor(2);
executor.next(task1).next(task2).next(task3).done(onDone);
task1 and task 2 should be executed in parallel while task3 should wait until one of the previous tasks finises. When all tasks finish onDone callback is executed.
I was trying to implement it using promises but I failed. I'm new to generators and currently have no idea if they can help here. This is for learning purposes mostly, that's why I don't want to use any third party libraries, just native JS. Any hint would be great, thanks in advance!