Wraps an async function so that concurrent calls with the same key share a single in-flight promise instead of starting duplicate operations.
Once the shared promise resolves (or rejects), subsequent calls will start a new operation.
The argument types of the wrapped function.
The return type of the wrapped function.
A function that computes a cache key from the arguments. Calls with the same key will be deduplicated.
The async function to wrap.
A deduplicated version of fn.
fn
import { dedupe } from '@cleverbrush/async';const fetchUser = dedupe( (id: number) => `user-${id}`, (id: number) => fetch(`/api/users/${id}`).then(r => r.json()));// These two calls share a single fetch:const [a, b] = await Promise.all([fetchUser(1), fetchUser(1)]); Copy
import { dedupe } from '@cleverbrush/async';const fetchUser = dedupe( (id: number) => `user-${id}`, (id: number) => fetch(`/api/users/${id}`).then(r => r.json()));// These two calls share a single fetch:const [a, b] = await Promise.all([fetchUser(1), fetchUser(1)]);
Wraps an async function so that concurrent calls with the same key share a single in-flight promise instead of starting duplicate operations.
Once the shared promise resolves (or rejects), subsequent calls will start a new operation.