thunky-with-args

1.0.0 • Public • Published

thunky-with-args

Like thunky but:

Delay the evaluation of a paramless variadic async function and cache the result.

npm status node Travis build status Dependency status JavaScript Style Guide

example

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 which works well for primitives, arrays and plain objects but takes a few shortcuts to be fast.

install

With npm do:

npm install thunky-with-args

license

MIT © Vincent Weevers

Package Sidebar

Install

npm i thunky-with-args

Weekly Downloads

998

Version

1.0.0

License

MIT

Unpacked Size

7.28 kB

Total Files

7

Last publish

Collaborators

  • vweevers