# memoize-weak-promise

> Garbage-collected memoizer for variadic async functions

Latest version **1.2.0** (published 2019-03-21) · ISC license · 0 weekly downloads

## Install

```sh
npm install memoize-weak-promise
pnpm add memoize-weak-promise
yarn add memoize-weak-promise
bun add memoize-weak-promise
```

## Health

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

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.2.0 |
| Published | 2019-03-21 |
| First published | 2019-03-18 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 7.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Kas van 't Veer |
| Maintainers | kazaakas |
| Keywords | memoize, weak, weakmap, garbage |

## Links

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

## Recent versions

- 1.2.0 (latest) — 2019-03-21
- 1.1.0 — 2019-03-19
- 1.0.0 — 2019-03-18

## README

# memoize-weak-promise
[![npm version](https://img.shields.io/npm/v/memoize-weak-promise.svg)](https://www.npmjs.com/package/memoize-weak-promise)

> Garbage-collected memoizer for variadic async functions

## Installation

```bash
npm install memoize-weak-promise
```

## Example

```js
import memoize from 'memoize-weak-promise';

var wrap0 = {value: 0};
var wrap1 = {value: 1};

const fn = memoize(a => new Promise(function rejectIfOdd(res, rej) {
    if (a.value % 2 === 1) setTimeout(() => rej(a.value), 1000);
    else setTimeout(() => res(a.value), 1000);
})); // Create a memoized function

var promise0 = fn(wrap0); // Returns a promise that will resolve to 0
var promise1 = fn(wrap1); // Returns a promise that will reject to 1

/* Will return cached versions if promises are pending: */
promise0 === fn(wrap0) // true
promise1 === fn(wrap1) // true

/* Will not cache rejected promises */
setTimeout(() => {
  promise0 === fn(wrap0) // true
  promise1 === fn(wrap1) // false, because the promise rejected
}, 1001);

/* Original arguments are now eligible for garbage collection: */
setTimeout(() => {
  wrap0 = wrap1 = undefined;
}, 1002);
```

## API

### function(fn [, options]) -> memoizedFn

Memoize a given function

#### arguments
- **fn** *(function)*: The function to memoize
- **options** *(Object)*:
  - **weak** *(boolean)*: Use weak references if possible (defaults to true)
  - **once** *(boolean)*: Only memoize one result

#### returns
- **memoizedFn** *(function)*: Promise that once the request finalizes will resolve in case of an OK status and reject in all other cases.
  - **clear** *(function(...args))*: Clear cached result for any arguments, or clear entire cache if no arguments provided


## Features

- Memoizes multiple arguments of any type
- Previous arguments are automatically garbage-collected when no longer referenced elsewhere
- No external dependencies
- Will not cache promises if they fail
- Compatible with ES5 and up

## How does `memoize-weak` differ from other memoize implementations?

Memoize functions cache the return value of a function, so that it can be used again without having to recalculate the value.

They do this by maintaining a cache of arguments that the function has previously been called with, in order to return results that correspond to an earlier set of arguments.

Usually this argument cache is retained indefinitely, or for a predefined duration after the original function call. This means that any objects passed as arguments are not eligible for garbage collection, even if all other references to these objects have been removed.

`memoize-weak` uses "weak references" to the argument values, so that once all the references to the arguments have been removed elsewehere in the application, the arguments will become eligible for cleanup (along with any cached return values that correspond to those arguments).

This allows you to use memoized functions with impunity, without having to worry about potential memory leaks.

## Using `memoize-weak` in ES5 applications

`memoize-weak` requires that `Map` and `WeakMap` are globally available. This means that these will have to be polyfilled for use in an ES5 environment.

Some examples of `Map` and `WeakMap` polyfills for ES5:

- [Babel Polyfill](https://babeljs.io/docs/usage/polyfill/)
- [CoreJS](https://github.com/zloirock/core-js)
- [`es6-map`](https://www.npmjs.com/package/es6-map) and [`es6-weak-map`](https://www.npmjs.com/package/es6-weak-map)

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