This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Semaphore(max) { | |
var counter = 0; | |
var waiting = []; | |
var take = function() { | |
if (waiting.length > 0 && counter < max){ | |
counter++; | |
let promise = waiting.shift(); | |
promise.resolve(); | |
} |