Memoize an Async Function
Write the code, see it run, pass the checks.
The problem
- hard
- Code
- Pro
Write memoizeAsync(fn), where fn takes one argument, a key, and returns a promise. Return a function that takes a key and returns a promise of fn's result for it. Call fn at most once per key: a call while the first one is still pending shares it rather than calling fn again, and once it has resolved, later calls get the same value without calling fn. Compare keys the way a Map does. If the promise rejects, every caller waiting on it gets that rejection, and the key is forgotten, so the next call for it tries fn again.
Your code must pass
- Shares one pending call between callers with the same key
- Reuses the value after it resolves
- Calls fn separately for each key
- Passes a rejection on and tries again next time