{"library":"semaphore","title":"Concurrency Semaphore for Node.js","description":"This package provides a basic counting semaphore mechanism for Node.js environments, designed to limit the number of concurrent operations accessing a shared resource. The current and only stable version, 1.1.0, was published 9 years ago and explicitly targets Node.js 0.8.0. Due to its age and lack of maintenance, it does not follow a regular release cadence and is effectively abandoned for modern Node.js development. Its primary differentiator is its extreme simplicity and minimal footprint, offering fundamental concurrency control without features found in more contemporary, promise-based semaphore implementations (e.g., `async-sema`, `await-semaphore`). While functional for very old projects, it lacks modern JavaScript features and type definitions out of the box.","language":"javascript","status":"abandoned","last_verified":"Sun Apr 19","install":{"commands":["npm install semaphore"],"cli":null},"imports":["const semaphore = require('semaphore');","const sem = require('semaphore')(capacity);","sem.take(function() { /* ... */ });"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"const semaphore = require('semaphore');\nconst http = require('http');\n\n// Limit concurrent database access to 1 operation at a time\nconst dbSemaphore = semaphore(1);\n\nconst expensive_database_operation = (callback) => {\n  console.log('Starting expensive DB operation...');\n  setTimeout(() => {\n    const error = Math.random() > 0.8 ? new Error('Database error!') : null;\n    console.log('Finished expensive DB operation.');\n    callback(error, 'Data from DB');\n  }, 1000);\n};\n\nconst server = http.createServer((req, res) => {\n  res.writeHead(200, { 'Content-Type': 'text/plain' });\n  dbSemaphore.take(function() {\n    console.log('Semaphore taken, requesting DB operation...');\n    expensive_database_operation(function(err, data) {\n      dbSemaphore.leave();\n      if (err) {\n        console.error('Request failed:', err.message);\n        return res.end(`Error: ${err.message}`);\n      }\n      res.end(`Success: ${data}`);\n    });\n  });\n});\n\nconst PORT = process.env.PORT ?? 3000;\nserver.listen(PORT, () => {\n  console.log(`Server listening on http://localhost:${PORT}`);\n  console.log('Try opening multiple tabs to http://localhost:3000 to see concurrency limits in action.');\n});\n","lang":"javascript","description":"This quickstart demonstrates how to create a semaphore to limit simultaneous database access within a Node.js HTTP server. It shows taking and leaving the semaphore using its callback-based API.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}