async-await-semaphore
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

async-await-semaphore

a semaphore provide two operations, historically denoted as P and V. Operation V increments the counter of a semaphore, and operation P decrements it.

API

new Semaphore(count:number = 1)

create a semaphore

import {Semaphore} from 'async-await-semaphore';

const semaphore = new Semaphore();

semaphore.p(I:number = 1)

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

semaphore.v(I:number = 1)

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

Readme

Keywords

Package Sidebar

Install

npm i async-await-semaphore

Weekly Downloads

7

Version

1.0.1

License

GPL-3.0

Unpacked Size

15 kB

Total Files

9

Last publish

Collaborators

  • francis-tao-jinjin