# cache-for-promise

> simple cache for promise-returning-functions.

Latest version **1.0.2** (published 2019-06-25) · MIT license · 0 weekly downloads

## Install

```sh
npm install cache-for-promise
pnpm add cache-for-promise
yarn add cache-for-promise
bun add cache-for-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.0.2 |
| Published | 2019-06-25 |
| First published | 2019-06-25 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 195.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | mosle |
| Maintainers | mosle |
| Keywords | cache, promise |

## Links

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

## Dependencies (1)

- [query-string](https://npm.io/package/query-string.md) ^6.8.1

## Alternatives

- [memory-cache](https://npm.io/package/memory-cache.md) — 795.0K weekly downloads
- [@httptoolkit/proxy-agent](https://npm.io/package/@httptoolkit/proxy-agent.md) — 11.2K weekly downloads
- [express-cache-controller](https://npm.io/package/express-cache-controller.md) — 5.3K weekly downloads
- [http-cache-middleware](https://npm.io/package/http-cache-middleware.md) — 4.5K weekly downloads
- [cache2](https://npm.io/package/cache2.md) — 1.5K weekly downloads

## Recent versions

- 1.0.2 (latest) — 2019-06-25
- 1.0.1 — 2019-06-25
- 1.0.0 — 2019-06-25

## README

# cache-for-promise
simple cache for promise-returning-functions.

## Install
```
npm install cache-for-promise
```


## Usage

basic usage
```
import CacheForPromise from "cache-for-promise"

let anyPromiseFunction = (arg)=>{
    console.log("any promise function is called");
    return new Promise((resolve,reject)=>{
        return resolve(arg);
    })
}

const ttl = 10;//seconds
const cfp = new CacheForPromise(anyPromiseFunction,ttl);

let arg = 1;
cfp.exec("cache-key-1",arg).then(result=>{
    console.log(result);//1
})
//"any promise function is called" is output


cfp.exec("cache-key-1",arg).then(result=>{
    console.log(result);//1
})
//key is same and cache is not expired, so "any promise function is called" is not output.

```

i.e with axios


```
import CacheForPromise from "cache-for-promise"
import axios from "axios"

const ttl = 10;//seconds
const cfp = new CacheForPromise(axios,ttl);

const axiosArguments = {
    method:"get",
    params:{test1:1,test2:2},
    url:"/any-url"
};//recommended for only get.

cfp.exec("cache-key-1",axiosArguments).then(result=>{
    console.log(result.data);
}).catch(error=>{
    console.log(error);
})

cfp.exec("cache-key-1",axiosArguments).then(result=>{
    console.log(result.data);
}).catch(error=>{
    console.log(error);
})


```

with auto cache key functions


```
import CacheForPromise from "cache-for-promise"
import KeyGenerator from "cache-for-promise/key-generator"
import axios from "axios"

const ttl = 10;//seconds
const cfp = new CacheForPromise(axios,ttl);

cfp.keygen = KeyGenerator.axios;//only for axios is prepared, now.

const axiosArguments = {
    method:"get",
    params:{test1:1,test2:2},
    url:"/any-url"
};//recommended for only get.

cfp.exec(axiosArguments).then(result=>{
    console.log(result.data);
}).catch(error=>{
    console.log(error);
})

cfp.exec(axiosArguments).then(result=>{
    console.log(result.data);
}).catch(error=>{
    console.log(error);
})

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