Skip to content

Mutex and Semaphore with timeouts for Typescript/NodeJS

License

Notifications You must be signed in to change notification settings

kevinoneill/wee-concurrent

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

33 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

@weegigs/concurrent

Utilities for dealing with concurrency in Typescript (and Javascript).

semantic-release Greenkeeper badge code style: prettier Maintainability

Overview

Concurrent provides two handy classes when you want to limit the amount of concurrent work being executed in Promises, Semaphore and Mutex.

Semaphore and Mutex share a common interface Gate. The Gate interface provides two functions acquire and execute.

acquire(timeout?: number): Promise<Release>

If a timeout greater than zero is passed then a TimeoutError will be triggered if the duration in milliseconds is exceeded.

try {
  const release = await gate.acquire(10);
  // ... do some work ...
  release();
} catch (error) {
  release();
}

execute<T>(worker: Worker<T>, timeout?: number): Promise<T>;

execute allows you to avoid managing the Release function by using a Worker. A Worker is a function from void to T or Promise<T>.

As with acquire, if a timeout greater than zero is passed then a TimeoutError will be triggered if the duration in milliseconds is exceeded.

try {
  const result = await gate.execute(() => {
    // ... do some work ...
    return value;
  }, 10);

  // ...do something with the value...
} catch (error) {
  // ... do something with the error ...
}

Todo

  • Semaphore
  • Mutex
  • Latch
  • Example Usage
  • Documentation

About

Mutex and Semaphore with timeouts for Typescript/NodeJS

Resources

License

Stars

Watchers

Forks

Packages

No packages published