Security News
ESLint is Now Language-Agnostic: Linting JSON, Markdown, and Beyond
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
concurrentp
Advanced tools
Promise concurrency control done right
Concurrency is a major concern for any asynchronous application. While Javascript's single-threaded nature solves many of the known issues with multithreaded runtime models, it does not relieve us from taking care of concurrency control; mutex violations can occure when performing io operations such as writing and reading from databases, and rollbacks can be caused by callbacks carried on with outdated, delayed network request.
ConcurrentP provides a modest and lightweight set of tools to overcome those issues, with as minimal a footprint as possible.
Add to your project by npm:
$ npm install -s concurrentp
or yarn:
$ yarn add concurrentp
Add to your code by importing any of the objects from the package:
import { Lock } from 'concurrentp';
Typescripts type declarations are included.
The Lock object provides means of mutual exclusion; Given two promises are performing a critical section of a singular operation, the lock object makes sure that no more than one promise can perform it at a time.
The Lock ensures mutual exclusion and starvation freedom, as it resumes promises in the order they acquired it.
Name | Return Value | Description |
---|---|---|
new Lock() | Lock | creates a new lock instance. |
acquire() | Promise | acquires a lock. Either resolves immediately, or when the lock frees up. |
release() | void | releases the lock, and possibly resolves the next promise which acquired the lock. |
import { Lock } from 'concurrentp';
const lock = new Lock();
async function oneAtATime() {
await lock.acquire();
await getIcecream();
lock.release();
}
The Semaphore object is used for limiting the capacity of a given operation. Much like the lock, it can be acquired and released; but unlike the latter, it allows for any given amount of parallel runs (instead of just 1 with lock).
Semaphore enforces a limit on the amount of parallel execution and provides starvation freedom, as it resumes promises in the order they acquired it.
Name | Return Value | Description |
---|---|---|
new Semaphore(limit: number) | Semaphore | creates a new semaphore that allows up to limit number of parallel executions. |
acquire() | Promise | acquires the semaphore. Either resolves immediately, or when the semaphores frees up. |
release() | void | releases the semaphore, and possibly resolves the next promise which acquired the semaphore. |
import { Semaphore } from 'concurrentp';
const semaphore = new Semaphore(2);
async function iHaveOnlyTwoHands() {
await semaphore.acquire();
await holdPotato();
semaphore.release();
}
The Queue object is used as rollback guard: it makes sure that results of recent instances of an operation cannot be overwritten by earlier ones. This is extremely useful in cases where one operation comes instead of another, but the other completed first, which can lead to a outdated result overwritting the most recent one.
The Queue provides you with a "tickets" system. Whenever you want to use a queue, acquire a ticket, and then go on and perform an async operation. When it's done, check if your ticket is still up to date - and if it is, it means it is safe for you to carry on and no rollback will occure.
The Queue object prevents rollbacks, but it does not prevent older promises from resolving, if they were the most recent ones to do so.
Name | Return Value | Description |
---|---|---|
new Queue() | Queue | creates a new queue instance. |
acquire() | number | instantly acquires a ticket. |
isMostRecent(ticket: number) | boolean | instantly checks if your ticket is the most recent one to complete. |
import { Queue } from 'concurrentp';
const queue = new Queue();
async function searchImage(animal) {
const ticket = queue.acquire();
const imageOfAnimal = await tumblr.search(animal);
if (queue.isMostRecent(ticket)) {
$('#image').src = imageOfAnimal;
}
}
searchImage('dog');
searchImage('ferret');
searchImage('cat');
The project is available to all under MIT license.
FAQs
Promise concurrency control done right
We found that concurrentp demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
Security News
Members Hub is conducting large-scale campaigns to artificially boost Discord server metrics, undermining community trust and platform integrity.
Security News
NIST has failed to meet its self-imposed deadline of clearing the NVD's backlog by the end of the fiscal year. Meanwhile, CVE's awaiting analysis have increased by 33% since June.