https://github.com/vweevers/thunky-with-args
Delay the evaluation of a variadic async function and cache the result
https://github.com/vweevers/thunky-with-args
async cache nodejs npm-package thunky
Last synced: 4 months ago
JSON representation
Delay the evaluation of a variadic async function and cache the result
- Host: GitHub
- URL: https://github.com/vweevers/thunky-with-args
- Owner: vweevers
- License: mit
- Created: 2018-04-28T10:54:18.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2020-05-25T04:07:37.000Z (about 6 years ago)
- Last Synced: 2025-10-27T04:39:05.916Z (8 months ago)
- Topics: async, cache, nodejs, npm-package, thunky
- Language: JavaScript
- Size: 7.81 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: readme.md
- License: LICENSE
Awesome Lists containing this project
README
# thunky-with-args
Like [thunky](https://github.com/mafintosh/thunky) but:
**Delay the evaluation of a ~~paramless~~ variadic async function and cache the result.**
[](https://www.npmjs.org/package/thunky-with-args)
[](https://www.npmjs.org/package/thunky-with-args)
[](http://travis-ci.org/vweevers/thunky-with-args)
[](https://david-dm.org/vweevers/thunky-with-args)
[](https://standardjs.com)
## example
```js
const dns = require('dns')
const memoize = require('thunky-with-args')
const lookup = memoize(function (hostname, opts, callback) {
if (typeof opts === 'function') {
callback = opts
opts = null
}
console.log('lookup', hostname, opts)
dns.lookup(hostname, opts, callback)
})
lookup('localhost', console.log)
lookup('localhost', console.log)
lookup('localhost', console.log)
lookup('localhost', { family: 6 }, console.log)
lookup('localhost', { family: 6 }, console.log)
```
This results in two lookups, the other calls are cached:
```
lookup localhost null
lookup localhost { family: 6 }
null '127.0.0.1' 4
null '127.0.0.1' 4
null '127.0.0.1' 4
null '::1' 6
null '::1' 6
```
## `thunk = thunkyWithArgs(work[, stringify])`
Returns a function `thunk` that caches the result of `work(..)` unless it returned an error. Optionally provide a `stringify` function that converts an array of arguments to a cache key. Defaults to [`sigmund`](https://github.com/isaacs/sigmund) which works well for primitives, arrays and plain objects but takes a few shortcuts to be fast.
## install
With [npm](https://npmjs.org) do:
```
npm install thunky-with-args
```
## license
[MIT](http://opensource.org/licenses/MIT) © Vincent Weevers