{"name":"promise-coalesce","version":"1.1.2","description":"Coalesces multiple promises for the same identifier into a single request","keywords":["promise","coalesce","async"],"author":{"name":"Doug Ayers","url":"https://github.com/douglascayers"},"repository":{"url":"git+https://github.com/douglascayers/promise-coalesce.git"},"bugs":{"url":"https://github.com/douglascayers/promise-coalesce/issues"},"homepage":"https://github.com/douglascayers/promise-coalesce#readme","license":"BSD-3-Clause","scripts":{"clean":"rm -rf dist && rm -rf coverage","prettier":"prettier --check --ignore-unknown --no-error-on-unmatched-pattern .","prettier:fix":"yarn prettier --write","lint":"eslint --ext .ts,.tsx,.js,.json .","lint:fix":"yarn lint --fix","lint:staged":"lint-staged --concurrent 1","format":"yarn prettier:fix && yarn lint:fix","build":"yarn clean && tsc","prepare":"yarn build && npx husky install","test":"jest","semantic-release":"semantic-release"},"release":{"branches":["main"],"plugins":["@semantic-release/commit-analyzer","@semantic-release/release-notes-generator","@semantic-release/changelog","@semantic-release/npm","@semantic-release/git","@semantic-release/github"]},"exports":"./dist/index.js","types":"./dist/index.d.ts","engines":{"node":">=16"},"dependencies":{},"devDependencies":{"@commitlint/cli":"^18.0.0","@commitlint/config-conventional":"^18.0.0","@semantic-release/changelog":"^6.0.3","@semantic-release/git":"^10.0.1","@types/jest":"^29.5.6","@types/node":"^18.17.19","@typescript-eslint/eslint-plugin":"^6.9.0","@typescript-eslint/parser":"^6.9.0","eslint":"^8.52.0","eslint-config-prettier":"^9.0.0","eslint-plugin-import":"^2.29.0","eslint-plugin-prettier":"^5.0.1","eslint-plugin-unused-imports":"^3.0.0","husky":"^8.0.3","jest":"^29.7.0","lint-staged":"^15.0.2","prettier":"^3.0.3","semantic-release":"^22.0.5","ts-jest":"^29.1.1","ts-node":"^10.9.1","typescript":"^5.2.2"},"commitlint":{"extends":["@commitlint/config-conventional"]},"lint-staged":{"*.{ts,tsx,js,json}":"yarn lint:fix","*":"yarn prettier:fix"},"_id":"promise-coalesce@1.1.2","gitHead":"f9d29734628ba5a2e61453d2df5246485d792dce","_nodeVersion":"18.19.0","_npmVersion":"10.1.0","dist":{"integrity":"sha512-zLaJ9b8hnC564fnJH6NFSOGZYYdzrAJn2JUUIwzoQb32fG2QAakpDNM+CZo1km6keXkRXRM+hml1BFAPVnPkxg==","shasum":"5d3bc4d0b2cf2e41e9df7cbeb6519b2a09459e3d","tarball":"http://registry-npm.ywwl.com/promise-coalesce/download/promise-coalesce-1.1.2.tgz","fileCount":12,"unpackedSize":12420,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQDk0HVQyfrXwaCXmao9PEZy+SQdAFmk5KABkNsw1kSmGwIgbJ4OvOUlgVatLDYhHv4bpyDIgPve54atIruPvtIrjmM="}],"size":4901,"noattachment":false,"key":"/promise-coalesce/-/promise-coalesce-1.1.2.tgz"},"_npmUser":{"name":"douglascayers","email":"douglascayers@gmail.com"},"directories":{},"maintainers":[{"name":"douglascayers","email":"douglascayers@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/promise-coalesce_1.1.2_1702955042599_0.16369499879146088"},"_hasShrinkwrap":false,"readme":"## promise-coalesce\n\nCoalesces multiple promises for the same identifier into a single request.\n\nReduces load on downstream systems when requests occur at the same time,\nwithout dropping requests or needing exclusion locks or wait-and-retry attempts.\n\n## Install\n\nWith npm:\n\n```sh\nnpm install promise-coalesce\n```\n\nWith yarn:\n\n```sh\nyarn add promise-coalesce\n```\n\n## Usage\n\n```ts\nimport { coalesceAsync } from 'promise-coalesce';\n\nawait coalesceAsync('some-group-key', async () => {\n  /* your logic */\n});\n```\n\n## Example\n\n**Cache Miss Relief Buffer**\n\n```ts\nimport { coalesceAsync } from 'promise-coalesce';\n\n// Imagine you want to retrieve a value, and you have a cache to speed things up.\n// If the value isn't in the cache, you'll need to get it from the source system,\n// which can be a time-consuming process like a database query or an API request.\nasync function getValue(cacheKey: string): Promise<YourData> {\n  // When multiple requests try to fetch the same value from the cache at the same time,\n  // because of the way async operations work, yielding the event loop at each `await`,\n  // then they will all try to get it from the cache.\n  let cachedValue = await cache.get(cacheKey);\n  // They will take turns checking the condition and all see that the value is missing.\n  if (!cachedValue) {\n    // Here's where `coalesceAsync` comes to the rescue!\n    // Instead of making multiple expensive calls to the source system,\n    // we use `coalesceAsync`` to ensure it's called only once, and other requests\n    // wanting the same cache key wait for the result.\n    cachedValue = await coalesceAsync<YourData>(cacheKey, async () => {\n      // Now, we fetch the value from the source system.\n      const sourceValue = await getSourceValue();\n      // We also cache it for future use.\n      await cache.set(cacheKey, sourceValue, ttl);\n      // Now, the value is in the cache, and future requests will avoid calling the source system\n      // until the cached data expires (based on TTL).\n      return sourceValue;\n    });\n  }\n  return cachedValue;\n}\n```\n\n## Credits\n\nThis solution is inspired by [node-cache-manager](https://github.com/node-cache-manager/node-cache-manager)'s\n[CallbackFiller](https://github.com/node-cache-manager/node-cache-manager/blob/4.1.0/lib/callback_filler.js) from the `v4.x` line.\n\nIt was [removed](https://github.com/node-cache-manager/node-cache-manager/issues/417)\nin the `v5.x` line, and `promise-coalesce` is an attempt to recover that feature\nwhile also being generic enough for other applications.\n","publish_time":1702955042821,"_cnpm_publish_time":1702955042821,"dist-tags":{"latest":"1.5.0"}}