New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Don't cache rejected promises - fixes #8 #10
Conversation
I like |
index.js
Outdated
}) | ||
.catch(() => { }); | ||
} else { | ||
cache.set(key, { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is duplicated. Can you extract it into an arrow function above the if
statement on line 36?
readme.md
Outdated
Type: `boolean`<br> | ||
Default: `false` | ||
|
||
Set to `true` if you want to cache rejected promises. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think Set to true if you want to
is moot. The default is defined, so we can just describe what it does:
Cache rejected promises.
test.js
Outdated
} | ||
|
||
return Promise.resolve(i); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be simplified by using an async function:
const memoized = m(async () => {
i++;
if (i === 1) {
throw new Error('foo bar');
}
return i;
}, {
cacheRejection: true
});
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Much better :)
Let's go with |
Updated |
Perfect 👌🌟 |
Hmm, it seems like this will wait to cache the value until the promise have settled, which is not how this module used to behave. I'm currently using the module something like this: const getPlanInfo = mem(async (planName) => {
const input = await expensiveApiCallThatFetchesPlanInfo()
return { currency: input.currency.toUpperCase(), amount: input.amount }
}, { maxAge: A_FEW_HOURS }) In 1.x every request for a specific plan would all return the same Promise and only one request would go out to the remote server. In 2.x there will be one request sent per call until the first Promise settles.
Would a PR to switch to returning the same pending |
Yes. This was just an oversight. |
Great 👍 There is one small problem though 🤔 There is currently no specification that the provided How do you want to solve this, should we add that it should provide edit: quick-lru also supports |
Submitted #12 which requires |
ping @sindresorhus 😄 |
This PR fixes #8 by not caching a rejected promise by default. I also added a
cacheRejection
option (couldn't come up with a better name) to re-enable it.Feedback welcome!