Created
April 7, 2026 10:57
-
-
Save haggen/85267325e4000b01c8175a19d91a97d9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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