{"name":"promise-coalesce","version":"1.3.0","keywords":["promise","coalesce","async"],"author":{"url":"https://github.com/douglascayers","name":"Doug Ayers"},"license":"BSD-3-Clause","_id":"promise-coalesce@1.3.0","maintainers":[{"name":"douglascayers","email":"douglascayers@gmail.com"}],"homepage":"https://github.com/douglascayers/promise-coalesce#readme","bugs":{"url":"https://github.com/douglascayers/promise-coalesce/issues"},"dist":{"shasum":"1a5f3b966b29c976ff2d49f71d913ec56e05ca4b","tarball":"http://registry-npm.ywwl.com/promise-coalesce/download/promise-coalesce-1.3.0.tgz","fileCount":9,"integrity":"sha512-2zaNnXKuGsUkkwC9S+3RHDgeHkvkaUVLPMJb9k9+Fg/wC14RwnMgm1HCZ1hU4EByv3Z8t8E58IVZFJHdNQYKhw==","signatures":[{"sig":"MEYCIQC8C8R7fCf6NvJwPfffYkfG8fIeauNM5Y5qGo6sEAjfrgIhANMBZTQc3qmnt9nVuyj7+HU63Q/Jf+hKrKlsShvL9wFN","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":10093,"size":4282,"noattachment":false,"key":"/promise-coalesce/-/promise-coalesce-1.3.0.tgz"},"types":"./dist/index.d.ts","engines":{"node":">=16"},"exports":"./dist/index.js","gitHead":"07f6903f95035b364b8081c6a95b107d62b1875a","release":{"plugins":["@semantic-release/commit-analyzer","@semantic-release/release-notes-generator","@semantic-release/changelog","@semantic-release/npm","@semantic-release/git","@semantic-release/github"],"branches":["main"]},"scripts":{"lint":"eslint --ext .ts,.tsx,.js,.json .","test":"jest","build":"yarn clean && tsc","clean":"rm -rf dist && rm -rf coverage","format":"yarn prettier:fix && yarn lint:fix","prepare":"yarn build && npx husky install","lint:fix":"yarn lint --fix","prettier":"prettier --check --ignore-unknown --no-error-on-unmatched-pattern .","lint:staged":"lint-staged --concurrent 1","prettier:fix":"yarn prettier --write","semantic-release":"semantic-release"},"_npmUser":{"name":"douglascayers","email":"douglascayers@gmail.com"},"commitlint":{"extends":["@commitlint/config-conventional"]},"repository":{"url":"git+https://github.com/douglascayers/promise-coalesce.git"},"_npmVersion":"11.6.2","description":"Coalesces multiple promises for the same identifier into a single request","directories":{},"lint-staged":{"*":"yarn prettier:fix","*.{ts,tsx,js,json}":"yarn lint:fix"},"_nodeVersion":"20.19.2","dependencies":{},"_hasShrinkwrap":false,"devDependencies":{"jest":"^29.7.0","husky":"^8.0.3","eslint":"^8.52.0","ts-jest":"^29.1.1","ts-node":"^10.9.1","prettier":"^3.0.3","typescript":"^5.2.2","@types/jest":"^29.5.6","@types/node":"^18.17.19","lint-staged":"^15.0.2","@commitlint/cli":"^18.0.0","semantic-release":"^22.0.5","eslint-plugin-import":"^2.29.0","@semantic-release/git":"^10.0.1","eslint-config-prettier":"^9.0.0","eslint-plugin-prettier":"^5.0.1","@typescript-eslint/parser":"^6.9.0","@semantic-release/changelog":"^6.0.3","eslint-plugin-unused-imports":"^3.0.0","@commitlint/config-conventional":"^18.0.0","@typescript-eslint/eslint-plugin":"^6.9.0"},"_npmOperationalInternal":{"tmp":"tmp/promise-coalesce_1.3.0_1762578270444_0.7268198478609622","host":"s3://npm-registry-packages-npm-production"},"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":1762578270689,"_cnpm_publish_time":1762578270689,"dist-tags":{"latest":"1.5.0"}}