need help in creating an analogue of the critical section in javascript, that is, the input of the next function in the critical section should be performed after the previous one, that's what is now:
class CriticalSection { constructor() { this._flag = false; } enter() { if (!this._flag) { return new Promise((resolve, reject) => { this._flag = true; setTimeout(() => resolve(), 3000); }); } else { //здесь нужно какое-то ограничение } } leav() { this._flag = false; } } let cs = new CriticalSection(); cs.enter().then((value) => { setTimeout(() => { console.log(`1`); cs.leav(); }, 2000) }, null); cs.enter().then((value) => { setTimeout(() => { console.log(`2`); cs.leav(); }, 2000) }, null);