Skip to content

Instantly share code, notes, and snippets.

@haggen
Created April 7, 2026 10:57
Show Gist options
  • Select an option

  • Save haggen/85267325e4000b01c8175a19d91a97d9 to your computer and use it in GitHub Desktop.

Select an option

Save haggen/85267325e4000b01c8175a19d91a97d9 to your computer and use it in GitHub Desktop.
const store = new Map<
() => unknown,
ReturnType<typeof Promise.withResolvers>
>();
/**
* Memoiza função assíncrona, garantindo que mais de 1 chamada concorrente retorne a mesma promessa.
*/
export function memo<T>(f: () => Promise<T>) {
return async () => {
let value = store.get(f);
if (!value) {
value = Promise.withResolvers<unknown>();
store.set(f, value);
value.resolve(await f());
}
return (await value.promise) as Promise<T>;
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment