promise-coalesce
Coalesces multiple promises for the same identifier into a single request.
Reduces load on downstream systems when requests occur at the same time,
without dropping requests or needing exclusion locks or wait-and-retry attempts.
Install
With npm:
npm install promise-coalesce
With yarn:
yarn add promise-coalesce
Usage
import { coalesceAsync } from 'promise-coalesce';
await coalesceAsync('some-group-key', async () => {
});
Example
Cache Miss Relief Buffer
import { coalesceAsync } from 'promise-coalesce';
async function getValue(cacheKey: string): Promise<YourData> {
let cachedValue = await cache.get(cacheKey);
if (!cachedValue) {
cachedValue = await coalesceAsync<YourData>(cacheKey, async () => {
const sourceValue = await getSourceValue();
await cache.set(cacheKey, sourceValue, ttl);
return sourceValue;
});
}
return cachedValue;
}
Credits
This solution is inspired by node-cache-manager's
CallbackFiller from the v4.x
line.
It was removed
in the v5.x
line, and promise-coalesce
is an attempt to recover that feature
while also being generic enough for other applications.