Skip to content

Conversation

Copy link

Copilot AI commented Oct 16, 2025

Fixes #53

This PR adds support for cache: 'while-pending' as a more readable alternative to cache: false for disabling result caching while still deduplicating concurrent in-flight requests.

Problem

Previously, to enable in-flight deduplication without persistent caching, users had to write:

pMemoize(fn, {cache: false /* only while pending */})

The cache: false syntax is not self-documenting and requires comments to clarify its behavior.

Solution

This PR implements cache: 'while-pending' as discussed in the issue comments:

pMemoize(fn, {cache: 'while-pending'})

This is much more readable and self-explanatory - it clearly indicates that results are only memoized while the promise is pending (in-flight), but not after resolution.

Implementation Details

  • The Options type now accepts cache: 'while-pending' in addition to false and cache storage objects
  • Internally, 'while-pending' is normalized to false for processing, maintaining all existing behavior
  • Both cache: false and cache: 'while-pending' behave identically - they disable persistent caching but still deduplicate concurrent requests
  • The change is fully backward compatible - existing code using cache: false continues to work unchanged

Changes

  • Updated TypeScript types to accept 'while-pending' as a cache option
  • Added normalization logic to treat 'while-pending' the same as false
  • Added comprehensive tests for the new option
  • Updated documentation in README

All tests pass (27/27) ✅

Original prompt

This section details on the original issue you should resolve

<issue_title>Export "in-flight only memoizer"</issue_title>
<issue_description>- Follows #20 (comment)

I don't know what this means:

pMemoize(fn, {cache: false})

I have to document it every time:

pMemoize(fn, {cache: false /* only while pending */})

The module can export a more readable helper instead:

export const pMemoizePending = (fn, opts) => pMemoize(fn, {...opts, cache: false})

Note: the types of opts should exclude cache

Some tags could also be added to package.json</issue_description>

<agent_instructions>create cache: "while-pending"</agent_instructions>

Comments on the Issue (you are @copilot in this section)

@sindresorhus I would prefer to fix main signature. Maybe a new option instead of setting `cache` to `false`? The problem with exporting a separate method is that we have to explain it doesn't have a `cache` setting and also need to add yet another method for a decorator version.

@Richienb Any thoughts on this?</comment_new>
<comment_new>@fregante
I don't think that's a big ask. The documentation would look like

memInFlight(fn, options)

Like mem, but just while pending. The options are the same except you can't pass a custom cache.


Same for the decorator. I do agree it's not optimal but… in the code it still reads better than any option could, IMHO</comment_new>
<comment_new>@fregante
I think cache: 'while-pending' is clear enough. "Pending" alone doesn't give enough information and still requires a comment</comment_new>
<comment_new>@sindresorhus
Let's go with cache: 'while-pending'.</comment_new>

Fixes #53


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI self-assigned this Oct 16, 2025
Copilot AI changed the title [WIP] Export in-flight only memoizer helper function Add support for cache: 'while-pending' option Oct 16, 2025
Copilot AI requested a review from fregante October 16, 2025 06:36
Copilot finished work on behalf of fregante October 16, 2025 06:36
Copy link
Collaborator

@fregante fregante left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sindresorhus since false and while-pending are equivalent, should we deprecate and then remove "false"?

@fregante fregante marked this pull request as ready for review October 16, 2025 06:38
@fregante fregante requested a review from sindresorhus October 16, 2025 06:38
@sindresorhus
Copy link
Owner

We can soft-deprecate it by removing it from the readme, but I don't see a strong urge to officially deprecate it for existing users yet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Export "in-flight only memoizer"

3 participants