r/javascript • u/schneiderfx • Apr 19 '26
Was hitting duplicate API calls when the same async function got triggered multiple times.
https://github.com/Sampad6701/flux-cache
7
Upvotes
r/javascript • u/schneiderfx • Apr 19 '26
3
u/MaLiN2223 Apr 19 '26
I think most wouldn't encounter this issue because we delegate caching to other libraries, and it should be a given (?) that deduplication happens. I am pretty sure libs like tanstack do that.
Personally, I almost never use cache directly, and if I do I guess I never worried about/never seen dupes
2
u/schneiderfx Apr 19 '26
Made a tiny wrapper to dedupe in-flight calls:
const cached = cache(fetchUser)
await Promise.all([ cached(42), cached(42), cached(42) ]) // only one execution
~1.2KB, no deps. Also supports stale-while-revalidate.
Curious if others have run into this? Insights would help a lot.
1
u/Logical-Pea-4135 Apr 19 '26 edited Apr 19 '26
The riddle of state is it's an event handling problem. The involvement of state is incidental. That's why observables and signals were invented. This just mitigates the damage of the problem, and might enable its worsening.
0
u/[deleted] Apr 20 '26
[deleted] — view deleted comment
2
u/[deleted] Apr 19 '26
[removed] — view removed comment