npm.io
0.0.3 • Published 4 years ago

await-blocker

Licence
MIT
Version
0.0.3
Deps
1
Size
17 kB
Vulns
0
Weekly
0
Stars
1

await-blockers

A simple typescript promise of semaphore, mutex, timeout.

Semaphore

Supports promise with semaphore.
Simply create Semaphore instance, and put critical sections between acquire() and release().
parameter of Semaphore's constructor is max number of concurrent tasks.
Context will blocked if concurrent task of semaphore is over max number until release() called.

Example

import { Semaphore } from 'await-blockers';
const semaphore = new Semaphore(2);
async semaphoreUsage(){
    await semaphore.acquire();
    try{
        ///... Some Critical Section
    } finally{
        await semaphore.release();
    }
}

Mutex

Supports promise with mutex.
It equals to Semaphore(1). Only allows one concurrent task.
Context will blocked until release() called.

Example
import { Mutex } from 'await-blockers';
const mutex = new Mutex();
async mutexUsage(){
    await mutex.acquire();
    try{
        ///... Some Critical Section
    } finally{
        await mutex.release();
    }
}

Timeout

Supports promise with timeout.
Context will blocked until timeout elapsed.

Example
import { waitFor } from 'await-blockers';
async timeOutUsage(){
    console.log("Waiting 1 seconds...");
    await waitFor(1000); //Will block context for 1 seconds..
    //... A Method which called after 1 seconds..
    console.log("Done!");
}

Keywords