a semaphore provide two operations, historically denoted as P and V. Operation V increments the counter of a semaphore, and operation P decrements it.
create a semaphore
import {Semaphore} from 'async-await-semaphore';
const semaphore = new Semaphore();
P operation
const semaphore = new Semaphore(0);
let started = 0;
let finished = 0;
async function thread() {
started += 1;
await semaphore.p(2);
finished += 1;
}
thread();
console.log(started); // 1
console.log(finished); // 0
V operation
const semaphore = new Semaphore(0);
let started = 0;
let finished = 0;
async function thread() {
started += 1;
await semaphore.p(2);
finished += 1;
}
thread();
console.log(started); // 1
console.log(finished); // 0
semaphore.v(2);
await sleep(10);
console.log(started); // 1
console.log(finished); // 1