Closed krystofwoldrich closed 3 years ago
PR welcome unless others disagree, but I think it's not an easy change and would require copying the whole of mem
to make this work since p-memoize
does not have access to mem
’s real cache data with the timestamp.
Alternatively, I suppose, it could also add max-age-map
here instead.
So, I've finnally got back to this and here is my first proposal on the mentioned change.
Let me know what you think. More description in the PR https://github.com/sindresorhus/p-memoize/pull/24.
Copy of https://github.com/sindresorhus/mem/issues/81 since here it's more appropriet as @fregante suggested.
I've noticed, that when memoizing long lasting async functions, the maxAge is evaluated relative to the moment when function call started. But in case of sync functions it is evaluated relative to the moment the function was resolved.
Expected: Sync functions as is. Async functions (functions that return promise) maxAge should be evaluated relative to when promise is resolved.
Current: Async functions maxAge is evaluated relative to the moment when Promise is returned. (Not resolved, as I would expect!)
Fix: Dirty fix is add await, but then all memoized function will be async. Clean fix is to change beaviour base on function return type. If Promise then wait till it's resolved. Else as is now.
I really appriciate your work, it's great library, just I can't use it because of this unexpected behaviour. Or if I miss understood something and it's working correctly I'm in advance sorry.