Zero dependency collection of composable asynchronous decorators.
- Request coalescing
- Concurrency limiting
- In-memory caching
- Jitter
- Rate limiting / Throttling
- Retry mechanism
💡 Have a feature idea? Raise an issue.
npm install @jacben/decoDeduplicate identical in-flight requests by combining them into one call.
import { coalesce } from "@jacben/deco";
// Example async function
const getUser = async (id) => {}
// Wrap the original function so requests with the same values are coalesced
const coalescedGetUser = coalesce(getUser);
// Only one call to getUser occurs, even though it is called twice.
await Promise.all([
coalescedGetUser(1),
coalescedGetUser(1),
]); If the values passed to the coalesced function are not strings, numbers or booleans, you'll need to provide a generateKey callback.
// Example async function, which takes an object as its input
const getPackage = async (pkg) => {}
// Return a key which uniquely identifies this input
const generateKey = (pkg) => `${pkg.name}@${pkg.version}`;
// Provide generateKey as an argument
const coalescedGetPackage = coalesce(getPackage, generateKey);
// You can now pass in the full pkg object
await coalescedGetPackage(pkg);
⚠️ Beware of collisions when dealing with user input.
For example, if your generateKey function is implemented as(...args) => args.join('|'),
thengenerateKey("a", "a")would have the same output asgenerateKey("a|a").
Limit how many requests can run concurrently.
import { limit } from "@jacben/deco";
// Example async function
const getUser = async (id) => {}
// Allow a maximum of 2 concurrent requests to getUser
const limitedGetUser = limit(getUser, 2);
// The third request will not start until the first or second finishes
await Promise.all([
limitedGetUser(1),
limitedGetUser(2),
limitedGetUser(3),
]);If you want to limit concurrent requests but allow identical requests to bypass this limit, you can combine decorators:
import { coalesce, limit } from "@jacben/deco"
let fn = async () => {}
fn = limit(fn, 5)
fn = coalesce(fn)The resulting chain would be: Request -> coalesce -> limit -> original function.
Any duplicate requests to coalesce do not hit to the next function in the chain. Therefore, the concurrency limit does not apply to identical requests.
In reverse, if you want to limit calls and then dedupe identical request, you would decorate in the reverse order:
import { coalesce, limit } from "@jacben/deco"
let fn = async () => {}
fn = coalesce(fn)
fn = limit(fn, 5)If you'd like to suggest a feature, report an issue or ask a question, feel free to raise an issue.