Skip to content

Commit

Permalink
add init value for semaphore
Browse files Browse the repository at this point in the history
  • Loading branch information
jacksbox committed Oct 7, 2019
1 parent efa37d4 commit a146ce2
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
};
var Semaphore = (function () {
function Semaphore(initialValue) {
if (initialValue === void 0) { initialValue = 1; }
this.value = initialValue;
this.queue = [];
}
Expand Down
18 changes: 18 additions & 0 deletions index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const semaphore = require('./index.js')

const { V, P } = semaphore(2)

const fn = async i => {
console.log('wait', i)
await P()
console.log('enter', i)
setTimeout(() => {
console.log('leave', i)
V()
}, 1000)
}

fn(1)
fn(2)
fn(3)
fn(4)

0 comments on commit a146ce2

Please sign in to comment.