https://github.com/floatdrop/memorize-promise
Cache promise result and update from time to time
https://github.com/floatdrop/memorize-promise
Last synced: about 1 year ago
JSON representation
Cache promise result and update from time to time
- Host: GitHub
- URL: https://github.com/floatdrop/memorize-promise
- Owner: floatdrop
- License: mit
- Created: 2015-10-03T04:56:33.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2015-10-05T06:48:52.000Z (over 10 years ago)
- Last Synced: 2025-03-07T09:37:13.667Z (about 1 year ago)
- Language: JavaScript
- Size: 141 KB
- Stars: 5
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: license
Awesome Lists containing this project
README
# memorize-promise [](https://travis-ci.org/floatdrop/memorize-promise)
Save promise result and update it from time to time.
## Install
```
$ npm install --save memorize-promise
```
## Usage
```js
const got = require('got');
const memorizePromise = require('memorize-promise');
const cache = memorizePromise(() => got('google.com'));
cache.then(res => console.log(res.body)); // Content 1
cache.then(res => console.log(res.body)); // Content 1
setTimeout(function () {
cache.then(res => console.log(res.body)); // Content 2
cache.then(res => console.log(res.body)); // Content 2
cache.stop(); // Stops updates
}, 5005);
```
## API
### memorizePromise(func, [options])
#### func
Type: `Function`
Factory of promises. It will be called for new promise with data every `updateInterval` milliseconds.
#### options
##### updateInterval
Type: `Number`
Default: `5000`
Update interval in milliseconds.
##### ttl
Type: `Number`
Default: `60000`
Lifetime of update timeout after last hit.
## License
MIT © [Vsevolod Strukchinsky](http://github.com/floatdrop)