UNPKG

thunky-with-args

Version:

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

23 lines (16 loc) 456 B
'use strict' const thunky = require('thunky') const sigmund = require('sigmund') module.exports = function (work, stringify) { stringify = stringify || sigmund return function (...args) { const callback = args.pop() const key = args.length === 0 ? '' : stringify(args) let thunk = this[key] if (thunk === undefined) { thunk = thunky(work.bind(null, ...args)) this[key] = thunk } thunk(callback) }.bind({}) }