UNPKG

jscoding

Version:

A envirement for coding and debugging javascript language.

25 lines (23 loc) 509 B
class Lock { constructor() { this.queue = []; this.locked = false; } async lock() { if (this.locked) { let that = this; await new Promise((resolve) => { that.queue.push(resolve); }); } this.locked = true; return true; } unlock() { this.locked = false; let resolve = this.queue.pop(); if (resolve) { resolve(); } } }