Registry / testing / synchd

synchd

JSON →
library1.1.0jsnpmunverified

A lightweight Promise-based mutex library (v1.1.0) that prevents concurrent execution of guarded code sections by queuing calls in order. Similar to node-synchronized but uses Promises natively and includes both TypeScript and Flow type definitions. It offers two main functions: synchd for one-off locking with a scope key, and synchdFn for creating wrapped functions that automatically serialize access. The library is minimal, has no dependencies, and follows a stable release cadence with simple, focused API.

npm install synchd
INSTALL
IMPORT
SIG · SYNCHD
S
synchd
testingjavascriptv1.1.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

synchd
import { synchd } from 'synchd'
const synchd = require('synchd'); // Needs .synchd to access
Default export is an object containing both functions; named import is preferred. For CommonJS, use require('synchd').synchd or destructure.
synchdFn
import { synchdFn } from 'synchd'
const synchdFn = require('synchd').synchdFn; // Works but verbose
Exported as named export from the module.
require('synchd') default object
const synchd = require('synchd'); const {synchd: lock, synchdFn} = synchd;
const lock = require('synchd'); // lock is an object, not a function
CommonJS default import yields an object containing both functions. You must destructure or use .synchd and .synchdFn.

Demonstrates using synchd() to guard a counter increment so three concurrent calls serialize correctly.

import { synchd } from 'synchd'; import delay from 'pdelay'; // Shared resource let counter = 0; const key = {}; async function increment() { // This section will not run concurrently with other synchd(key) calls await synchd(key, async () => { const current = counter; await delay(10); // simulate async work counter = current + 1; }); } // Run multiple increments concurrently await Promise.all([increment(), increment(), increment()]); console.log(counter); // 3, not 1 or 2
Debug
Known issues
gotchaThe scopeKey must be the same object reference to share a lock; using a primitive or different object instances will not lock correctly.
fix
Use a shared object (e.g., const key = {};) or a Symbol as the scopeKey. Strings work only if compared by value (they are).
affects: >=1.0.0
gotchaIf the fn does not return a Promise, behavior is undefined; the library expects a Promise.
fix
Ensure fn is an async function or explicitly returns a Promise.
affects: >=1.0.0
breakingIn v1.0.0, the default export was a function, but v1.1.0 changed it to an object containing both synchd and synchdFn. Old code using import synchd from 'synchd' will break.
fix
Change import to import { synchd } from 'synchd' or use require('synchd').synchd.
affects: 1.1.0
Errors
Common errors & fixes
TypeError: synchd is not a function
Using require('synchd') directly and calling it as a function, but the default CommonJS export is an object.
fix
Use const { synchd } = require('synchd'); or const synchd = require('synchd').synchd;
Uncaught TypeError: fn is not a function
Passing a non-function (or undefined) as the second argument to synchd().
fix
Ensure the second argument is a function that returns a Promise, e.g., async () => { ... }.
Cannot find module 'synchd'
Package not installed.
fix
Run npm install synchd.
Upgrade
Version history
1.1.0latest on npm
Audit
Dependencies
pdelayoptionalUsed in synchdFn example for async delay; not a runtime dependency
Agent activity
12 hits · last 30 days
node
10
Amazon
1
Resources
packagesynchd
synchd — npm install synchd · libregistry