{"_id":"@algonomia/semaphore-async-await","_rev":"1-bf05241ea4cb9dee899cdfa13d2bf58e","name":"@algonomia/semaphore-async-await","dist-tags":{"latest":"1.0.0"},"versions":{"1.0.0":{"name":"@algonomia/semaphore-async-await","version":"1.0.0","author":{"name":"Jan Soendermann","email":"jan.soendermann+npm@gmail.com"},"contributors":[{"name":"Gergely Gombos","email":"gombosg+npm@gmail.com"},{"name":"Brice Guillaume","email":"brice.guillaume@algonomia.com"}],"homepage":"https://github.com/Algonomia/semaphore-async-await","repository":{"type":"git","url":"git+ssh://git@github.com/Algonomia/semaphore-async-await.git"},"license":"MIT","scripts":{"build":"tsc","generate-docs":"typedoc --tsconfig ./tsconfig-typedoc.json --theme minimal --mode file --module system --out docs ./Semaphore.ts","prepublish":"npm run build","test":"jest","lint":"tslint 'src/**/*.ts'","prepush":"npm run lint"},"engines":{"node":">=4.1"},"main":"./dist/index.js","typings":"./dist/index.d.ts","devDependencies":{"@types/jest":"^19.2.2","husky":"^0.13.2","jest":"^19.0.2","ts-jest":"^19.0.2","tslint":"^4.5.1","typedoc":"^0.5.7","typescript":"^2.2.1"},"jest":{"transform":{".(ts|tsx)":"<rootDir>/node_modules/ts-jest/preprocessor.js"},"testRegex":"(/__tests__/.*|\\.(test|spec))\\.(ts|tsx|js)$","moduleFileExtensions":["ts","tsx","js"]},"gitHead":"08d96544ea350120857cdc7affb8c141f5059693","description":"A promise-based semaphore implementation suitable to be used with async/await. (This fork includes a function to wait for all queued promises to finish.)","bugs":{"url":"https://github.com/Algonomia/semaphore-async-await/issues"},"_id":"@algonomia/semaphore-async-await@1.0.0","_npmVersion":"6.4.1","_nodeVersion":"9.4.0","_npmUser":{"name":"brice.guillaume","email":"brice.guillaume@algonomia.com"},"dist":{"integrity":"sha512-VH4UcYcDoa+qC6eQxIaxUOrCpK/m+w/lKycjvFzmOT1skPOKvRMo0cg/NZxHYYzPbkrW546NtuwMXSZ0sqVm+A==","shasum":"d6ce539a5e92e925029614e53a8e6e6330a21b4b","tarball":"https://registry.npmjs.org/@algonomia/semaphore-async-await/-/semaphore-async-await-1.0.0.tgz","fileCount":15,"unpackedSize":42929,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJbztzyCRA9TVsSAnZWagAAaJwP/2Z88gRI5BQnYwgdH3qs\nuNH/LI5CGbwPZIAQhCv+YvKUt0Tk9V6ds7GLESjnj2lUwzM6xRmKzSZNOdfo\nR6LgktowqeRJYPr1n+BRcs9KTkBKWovSqpfVGRea8ms8nASU5Lw7BDDkuBBP\nI4+F/IRls6dPn+xo4EOkFxSZtI8wGBYmAHWOBeQ6kSKSPYSwAMV7H3gOKUfm\nEbaVCizN6egL6zbN0gtTeasKU7Eq3WOrjJnfbuNvOymhX1KFjsfh39DJFg7o\n84NeqhAb0nRChNXgHU38iS7AO4ozALmZTuH1DG0zHAxAzLre7CV8dFm5/Ri1\nIwvFZGZFKOx3q1FdSgSWHTdJUjAovh82PFkufqo+yuQ9tkVyDhPKvJTgqf0w\na+m8q9UdFI1agtMsZe+JMJskjS6G6f2mloE1e3iqyPZsAXQnP5gJUc0cHzPN\nv/GAl+XBsUHrn11J1bcjCZ801N9Ja+/Zl457FEO/pXqN+ky1Tq7rBJ9DilFc\ncaAv2teoIU75ce+z097QrbmiBZ8SIOKYCvlt6hapRD36u3e+LOqfbpR6qH76\nLkPe/zSL7Ro6hdFwxEZBuRIPGBdv9AbLuZPgL/rls2AszUmXQdtnATdTkC4q\nBfS6PVdAOTurDNNd2dFtmsk8qkGdrW7LBRZzeN/jE4wnCfwhjkfCe0DVl25g\net2k\r\n=FGoO\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIHIcvhH+Ks4CS82omqpuNV45oQCpzWyZt2PYgUHiVTf0AiAne1OzYfrTr11WqtSyFPRhQkHulP3Qjk2Gyj8XR5ImlQ=="}]},"maintainers":[{"name":"brice.guillaume","email":"brice.guillaume@algonomia.com"}],"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/semaphore-async-await_1.0.0_1540283633562_0.9757195360990654"},"_hasShrinkwrap":false}},"time":{"created":"2018-10-23T08:33:53.379Z","1.0.0":"2018-10-23T08:33:53.706Z","modified":"2022-04-04T12:52:28.229Z"},"maintainers":[{"name":"brice.guillaume","email":"brice.guillaume@algonomia.com"}],"description":"A promise-based semaphore implementation suitable to be used with async/await. (This fork includes a function to wait for all queued promises to finish.)","homepage":"https://github.com/Algonomia/semaphore-async-await","repository":{"type":"git","url":"git+ssh://git@github.com/Algonomia/semaphore-async-await.git"},"contributors":[{"name":"Gergely Gombos","email":"gombosg+npm@gmail.com"},{"name":"Brice Guillaume","email":"brice.guillaume@algonomia.com"}],"author":{"name":"Jan Soendermann","email":"jan.soendermann+npm@gmail.com"},"bugs":{"url":"https://github.com/Algonomia/semaphore-async-await/issues"},"license":"MIT","readme":"# JavaScript Semaphore\n\nA promise-based semaphore implementation suitable to be used with async/await. (This fork includes a function to wait for all queued promises to finish.)\n\n## Spare me the details, all I need is a lock.\nJust ```import { Lock } from 'semaphore-async-await'```, acquire the lock by calling ```await lock.acquire()``` and release it when you're done by calling ```lock.release()```.\n\n## But JavaScript is single-threaded and doesn't need semaphores!\nThis package can be used to synchronize functions that span multiple iterations of the event loop and prevent other code from being executed while your function is waiting.\n\nSuppose you have a function\n\n```javascript\nasync function criticalFunction() {\n  const data = await getDataFromDb();\n  const modifiedData = await asynchronouslyDoStuffWithData(data);\n  return writeDataBackToDb(modifiedData);\n}\n```\n\nCalling this function repeatedly could lead to overlapping read/writes. To avoid this problem, a lock can be added like so:\n\n```javascript\nconst lock = new Semaphore(1);\n\nasync function criticalFunctionSynchronous() {\n  await lock.acquire();\n\n  try {\n    await criticalFunction();\n  } finally {\n    lock.release();\n  }\n}\n```\n\nAsynchronous functions like ```criticalFunction``` are executed in multiple chunks of code on the event loop, this package makes it possible to enforce an ordering in these chunks.\n\n## Install\n```yarn add semaphore-async-await```\n\n[<h2>API</h2>](http://jsoendermann.github.io/semaphore-async-await/classes/semaphore.html)\n\n## Usage\n```javascript\nimport Semaphore from 'semaphore-async-await';\n\n(async () => {\n\n  // A Semaphore with one permit is a lock\n  const lock = new Semaphore(1);\n\n  // Helper function used to wait for the given number of milliseconds\n  const wait = (ms) => new Promise(r => setTimeout(r, ms));\n\n  let globalVar = 0;\n\n  (async () => {\n    // This waits (without blocking the event loop) until a permit becomes available\n    await lock.wait();\n    const localCopy = globalVar;\n    await wait(500);\n    globalVar = localCopy + 1;\n    // Signal releases the lock and lets other things run\n    lock.signal();\n  })();\n\n  // This returns false because the function above has acquired the lock\n  // and is scheduled to continue executing once the main function yields or\n  // returns\n  console.log(lock.tryAcquire() === false);\n\n  // Similar to the function above but using waitFor instead of wait. We\n  // give it five seconds to wait which is enough time for it to acquire\n  // the lock\n  (async () => {\n    // This waits for at least five seconds, trying to acquire a permit.\n    const didAcquireLock = await lock.waitFor(5000);\n    if (didAcquireLock) {\n      const localCopy = globalVar;\n      await wait(500);\n      globalVar = localCopy + 1;\n      // Signal releases the lock and lets other things run\n      lock.signal();\n    }\n  })();\n\n  // Alternative to using wait()/signal() directly\n  lock.execute(async () => {\n    const localCopy = globalVar;\n    await wait(500);\n    globalVar = localCopy + 1;\n  });\n\n  // Wait for everything to finish\n  await lock.finishAll();\n\n  console.log(globalVar === 3);\n})();\n```\n\n## License\nMIT\n","readmeFilename":"README.md"}