npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

concurrentp

v1.0.3

Published

Promise concurrency control done right

Downloads

21

Readme

Logo

ConcurrentP

Promise concurrency control done right

Travis npm

Abstract

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.

Usage

Installing

Add to your project by npm:

$ npm install -s concurrentp

or yarn:

$ yarn add concurrentp

Importing

Add to your code by importing any of the objects from the package:

import { Lock } from 'concurrentp';

Typings

Typescripts type declarations are included.

API

Contents

Lock

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.

Methods:

| 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. |

Example:

import { Lock } from 'concurrentp';

const lock = new Lock();

async function oneAtATime() {
	await lock.acquire();

	await getIcecream();
	
	lock.release();
}

Semaphore

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.

Methods:

| 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. |

Example

import { Semaphore } from 'concurrentp';

const semaphore = new Semaphore(2);

async function iHaveOnlyTwoHands() {
	await semaphore.acquire();

	await holdPotato();
	
	semaphore.release();
}

Queue

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.

Methods:

| 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. |

Example

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');

License

The project is available to all under MIT license.