{"id":46572,"library":"synchd","title":"synchd","description":"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.","status":"active","version":"1.1.0","language":"javascript","source_language":"en","source_url":"https://github.com/Macil/synchd","tags":["javascript","mutex","asynchronous","queue","promise"],"install":[{"cmd":"npm install synchd","lang":"bash","label":"npm"},{"cmd":"yarn add synchd","lang":"bash","label":"yarn"},{"cmd":"pnpm add synchd","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Used in synchdFn example for async delay; not a runtime dependency","package":"pdelay","optional":true}],"imports":[{"note":"Default export is an object containing both functions; named import is preferred. For CommonJS, use require('synchd').synchd or destructure.","wrong":"const synchd = require('synchd'); // Needs .synchd to access","symbol":"synchd","correct":"import { synchd } from 'synchd'"},{"note":"Exported as named export from the module.","wrong":"const synchdFn = require('synchd').synchdFn; // Works but verbose","symbol":"synchdFn","correct":"import { synchdFn } from 'synchd'"},{"note":"CommonJS default import yields an object containing both functions. You must destructure or use .synchd and .synchdFn.","wrong":"const lock = require('synchd'); // lock is an object, not a function","symbol":"require('synchd') default object","correct":"const synchd = require('synchd'); const {synchd: lock, synchdFn} = synchd;"}],"quickstart":{"code":"import { synchd } from 'synchd';\nimport delay from 'pdelay';\n\n// Shared resource\nlet counter = 0;\nconst key = {};\n\nasync function increment() {\n  // This section will not run concurrently with other synchd(key) calls\n  await synchd(key, async () => {\n    const current = counter;\n    await delay(10); // simulate async work\n    counter = current + 1;\n  });\n}\n\n// Run multiple increments concurrently\nawait Promise.all([increment(), increment(), increment()]);\nconsole.log(counter); // 3, not 1 or 2","lang":"typescript","description":"Demonstrates using synchd() to guard a counter increment so three concurrent calls serialize correctly."},"warnings":[{"fix":"Use a shared object (e.g., const key = {};) or a Symbol as the scopeKey. Strings work only if compared by value (they are).","message":"The scopeKey must be the same object reference to share a lock; using a primitive or different object instances will not lock correctly.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Ensure fn is an async function or explicitly returns a Promise.","message":"If the fn does not return a Promise, behavior is undefined; the library expects a Promise.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Change import to import { synchd } from 'synchd' or use require('synchd').synchd.","message":"In 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.","severity":"breaking","affected_versions":"1.1.0"}],"env_vars":null,"search_vec":"'access':64 'api':81 'asynchron':84 'automat':62 'base':6 'cadenc':77 'call':20 'code':16 'concurr':12 'creat':58 'definit':39 'depend':71 'execut':13 'flow':37 'focus':80 'follow':73 'function':44,60 'guard':15 'includ':33 'javascript':82 'key':54 'librari':8,66 'lightweight':3 'lock':50 'main':43 'minim':68 'mutex':7,83 'nativ':31 'node':26 'node-synchron':25 'offer':41 'one':48 'one-off':47 'order':22 'prevent':11 'promis':5,30,86 'promise-bas':4 'queu':19 'queue':85 'releas':76 'scope':53 'section':17 'serial':63 'similar':23 'simpl':79 'stabl':75 'synchd':1,45 'synchdfn':56 'synchron':27 'two':42 'type':38 'typescript':35 'use':29 'v1.1.0':9 'wrap':59","created_at":"2026-06-07T13:00:34.671745+00:00","updated_at":"2026-06-07T13:00:34.671745+00:00","problems":[{"fix":"Use const { synchd } = require('synchd'); or const synchd = require('synchd').synchd;","cause":"Using require('synchd') directly and calling it as a function, but the default CommonJS export is an object.","error":"TypeError: synchd is not a function"},{"fix":"Ensure the second argument is a function that returns a Promise, e.g., async () => { ... }.","cause":"Passing a non-function (or undefined) as the second argument to synchd().","error":"Uncaught TypeError: fn is not a function"},{"fix":"Run npm install synchd.","cause":"Package not installed.","error":"Cannot find module 'synchd'"}],"ecosystem":"npm","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":null,"cli_version":null,"type":"library","homepage":"https://github.com/Macil/synchd#readme","github":"https://github.com/Macil/synchd","docs":null,"changelog":null,"pypi":null,"npm":"synchd","openapi_spec":null,"status_page":null,"smithery":null,"categories":["testing","devops"],"base_url":null,"auth_type":null,"provenance":{"verified_status":null,"verified_at":null,"last_verified":"2026-06-07","next_check":"2026-09-05","install_tag":null}}