Skip to content

Commit

Permalink
docs: add example about call deduping
Browse files Browse the repository at this point in the history
  • Loading branch information
redabacha committed Oct 12, 2024
1 parent 9949555 commit 11da523
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
13 changes: 10 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@ additional built-in features and changes such as:
- 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) + commonjs, deno and
browser support!
[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).
[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
Expand Down Expand Up @@ -59,6 +60,12 @@ const getRedditFeed = rememberPromise(

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
Expand Down
2 changes: 1 addition & 1 deletion mod.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ afterEach(() => {
time.restore();
});

it("should throttle calls to promiseFn", async () => {
it("should dedupe calls to promiseFn", async () => {
const promiseFn = createMockPromiseFn();

const cachedPromiseFn = rememberPromise(promiseFn);
Expand Down
6 changes: 6 additions & 0 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@
*
* 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"),
* ]);
* ```
*/

Expand Down

0 comments on commit 11da523

Please sign in to comment.