кстати, в js самафоры есть какие?
class PromiseGroup {
constructor() {
this.resolve = null;
this.reject = null;
this.promise = new Promise((resolve, reject) => {
this.resolve = resolve;
this.reject = reject;
});
}
async wait() {
return this.promise;
}
}
class WaitFor {
constructor() {
this.promiseGroupArray = [];
}
set() {
for(const p of this.promiseGroupArray)
p.resolve();
this.promiseGroupArray = [];
}
async wait() {
const p = new PromiseGroup();
this.promiseGroupArray.push(p);
return p.wait();
}
}
const sleep = ms => new Promise(r => setTimeout(r, ms));
(async () => {
const w = new WaitFor();
for(let j = 0; j < 10; j++) {
let seq = 0;
let f = async () => {
await w.wait();
console.log('seq: %s/%s', j, seq++);
};
for(let i = 0; i < 10; i++)
f();
await sleep(2e3);
w.set();
}
})();