# remember-promise

> Remembering promises that were made!

Latest version **2.0.4** (published 2024-10-12) · MIT license · 0 weekly downloads

## Install

```sh
npm install remember-promise
pnpm add remember-promise
yarn add remember-promise
bun add remember-promise
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.4 |
| Published | 2024-10-12 |
| First published | 2022-09-10 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 30.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 44 |
| Author | Reda Bacha |
| Maintainers | rbacha |

## Links

- npm: https://www.npmjs.com/package/remember-promise
- Repository: https://github.com/redabacha/remember-promise
- Homepage: https://github.com/redabacha/remember-promise#readme
- Issues: https://github.com/redabacha/remember-promise/issues
- npm.io page: https://npm.io/package/remember-promise

## Recent versions

- 2.0.4 (latest) — 2024-10-12
- 2.0.3 — 2024-03-26
- 2.0.2 — 2024-03-09
- 2.0.1 — 2024-03-07
- 2.0.0 — 2024-03-07
- 1.10.2 — 2024-03-06
- 1.10.1 — 2024-03-05
- 1.10.0 — 2024-03-02
- 1.9.1 — 2024-02-16
- 1.9.0 — 2024-02-16
- 1.8.0 — 2024-02-12
- 1.7.0 — 2024-02-12
- 1.6.0 — 2024-02-11
- 1.5.1 — 2024-02-11
- 1.5.0 — 2024-02-11
- … 6 more at https://npm.io/package/remember-promise/versions

## README

# remember-promise

[![bundle size](https://pkg-size.dev/badge/bundle/852)](https://pkg-size.dev/remember-promise)
[![codecov](https://codecov.io/gh/redabacha/remember-promise/graph/badge.svg?token=KMRTWA2DHQ)](https://codecov.io/gh/redabacha/remember-promise)
[![JSR score](https://jsr.io/badges/@reda/remember-promise/score)](https://jsr.io/@reda/remember-promise/score)
[![JSR](https://jsr.io/badges/@reda/remember-promise)](https://jsr.io/@reda/remember-promise)
[![npm](https://shields.io/npm/v/remember-promise)](https://www.npmjs.com/package/remember-promise)
[![license](https://shields.io/github/license/redabacha/remember-promise)](https://github.com/redabacha/remember-promise/blob/main/LICENSE)

A simple utility to remember promises that were made! It is greatly inspired by
the [p-memoize](https://github.com/sindresorhus/p-memoize) utility but with
additional built-in features and changes such as:

- ttl expiry
- stale-while-revalidate behavior
- ability to ignore results from being cached
- [optimal probabilistic cache stampede prevention](https://cseweb.ucsd.edu/~avattani/papers/cache_stampede.pdf)
- zero dependencies +
  [tiny bundle size](https://pkg-size.dev/remember-promise) + universal runtime
  support!

## Installation

remember-promise is available on both
[npm](https://www.npmjs.com/package/remember-promise) and
[JSR](https://jsr.io/@reda/remember-promise). The npm package is published as
CommonJS for maximum compatibility.

To use from npm, install the
[remember-promise](https://www.npmjs.com/package/remember-promise) package and
then import into a module:

```js
import { rememberPromise } from "remember-promise";
```

To use from JSR, install the
[@reda/remember-promise](https://jsr.io/@reda/remember-promise) package and then
import into a module:

```js
import { rememberPromise } from "@reda/remember-promise";
```

## Usage

```js
import { rememberPromise } from "remember-promise";

const getRedditFeed = rememberPromise(
  (subreddit) =>
    fetch(`https://www.reddit.com/r/${subreddit}.json`).then((res) =>
      res.json()
    ),
  {
    ttl: 300_000, // 5 minutes before the result must be revalidated again
    /* see below for a full list of available options */
  },
);

const firstResult = await getRedditFeed("all");
const secondResult = await getRedditFeed("all"); // this call is cached

// only one http request is made
const [thirdResult, fourthResult] = await Promise.all([
  getRedditFeed("javascript"),
  getRedditFeed("javascript"),
]);
```

## Options

| Name                 | Description                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         |
| :------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `ttl`                | Configures how long in milliseconds the cached result should be used before needing to be revalidated. Additionally, setting this value to zero or a negative number will disable caching. **NOTE: the actual revalidation of the cached result is done slightly before expiry by default. This can be adjusted using the `xfetchBeta` option.** By default this is `Infinity` so the cached result will be used indefinitely.                                                                                                                                                                                                      |
| `allowStale`         | This enables stale-while-revalidate behavior where an expired result can still be used while waiting for it to be updated in the background asynchronously. By default this is set to `true` so the behavior is enabled.                                                                                                                                                                                                                                                                                                                                                                                                            |
| `cache`              | This is where cached results will be stored. It can be anything you want such as [lru-cache](https://github.com/isaacs/node-lru-cache) or a redis backed cache as long as it implements a `get` and `set` method defined in the `RememberPromiseCache` type. If you would like to disable caching and only deduplicate identical concurrent calls instead then set this to `false`. When this is set to `false`, the `onCacheUpdateError` and `shouldIgnoreResult` options will be never be used. By default this is a new instance of [Map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map). |
| `getCacheKey`        | Identical behavior to the `cacheKey` option in [p-memoize](https://github.com/sindresorhus/p-memoize#cachekey) except that it's allowed to return a promise. It should return what the cache key is based on the parameters of the given function. By default this will serialize all arguments using `JSON.stringify`.                                                                                                                                                                                                                                                                                                             |
| `onCacheUpdateError` | Use this to catch errors when attempting to update the cache or if `shouldIgnoreResult` throws an error. By default this is `undefined` which means any errors will be rethrown as an unhandled promise rejection.                                                                                                                                                                                                                                                                                                                                                                                                                  |
| `shouldIgnoreResult` | Determines whether the returned result should be added to the cache. By default this is `undefined` which means it will always use the returned result for caching.                                                                                                                                                                                                                                                                                                                                                                                                                                                                 |
| `xfetchBeta`         | This is the beta value used in [optimal probabilistic cache stampede prevention](https://cseweb.ucsd.edu/~avattani/papers/cache_stampede.pdf) where values more than 1 favors earlier revalidation while values less than 1 favors later revalidation. By default this is set to 1 so the revalidation of a cached result will happen at a random time slightly before expiry. **If you wish to opt-out of this behavior, then set this value to 0.**                                                                                                                                                                               |

---
_Source: https://npm.io/package/remember-promise · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
