{"_id":"thunky","_rev":"196839","name":"thunky","description":"delay the evaluation of a paramless async function and cache the result","dist-tags":{"latest":"1.1.0"},"maintainers":[{"name":"feross","email":""},{"name":"mafintosh","email":""}],"time":{"modified":"2021-06-03T12:29:51.000Z","created":"2013-03-06T18:30:54.142Z","1.1.0":"2019-10-14T08:54:30.361Z","1.0.3":"2018-10-16T17:49:48.229Z","1.0.2":"2017-02-12T00:48:28.724Z","1.0.1":"2016-07-15T10:27:21.115Z","1.0.0":"2016-07-15T10:26:22.935Z","0.1.0":"2013-03-06T18:30:54.142Z"},"users":{"bret":true,"ziflex":true,"quocnguyen":true},"author":{"name":"Mathias Buus Madsen","email":"mathiasbuus@gmail.com"},"repository":{"type":"git","url":"git://github.com/mafintosh/thunky.git"},"versions":{"1.1.0":{"name":"thunky","version":"1.1.0","description":"delay the evaluation of a paramless async function and cache the result","main":"index.js","devDependencies":{"standard":"^12.0.1","tape":"^4.9.1"},"repository":{"type":"git","url":"git://github.com/mafintosh/thunky.git"},"scripts":{"test":"standard && tape test.js"},"keywords":["memo","thunk","async","lazy","control","flow","cache"],"author":{"name":"Mathias Buus Madsen","email":"mathiasbuus@gmail.com"},"bugs":{"url":"https://github.com/mafintosh/thunky/issues"},"homepage":"https://github.com/mafintosh/thunky#readme","license":"MIT","gitHead":"4f24392fd09949e3785598d99b087e8256599989","_id":"thunky@1.1.0","_nodeVersion":"10.16.3","_npmVersion":"6.9.0","dist":{"shasum":"5abaf714a9405db0504732bbccd2cedd9ef9537d","size":3117,"noattachment":false,"key":"/thunky/-/thunky-1.1.0.tgz","tarball":"http://registry.cnpm.dingdandao.com/thunky/download/thunky-1.1.0.tgz"},"maintainers":[{"name":"feross","email":""},{"name":"mafintosh","email":""}],"_npmUser":{"name":"mafintosh","email":"mathiasbuus@gmail.com"},"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/thunky_1.1.0_1571043270184_0.776382449698759"},"_hasShrinkwrap":false,"publish_time":1571043270361,"_cnpm_publish_time":1571043270361},"1.0.3":{"name":"thunky","version":"1.0.3","description":"delay the evaluation of a paramless async function and cache the result","main":"index.js","devDependencies":{"standard":"^12.0.1","tape":"^4.9.1"},"repository":{"type":"git","url":"git://github.com/mafintosh/thunky.git"},"scripts":{"test":"standard && tape test.js"},"keywords":["memo","thunk","async","lazy","control","flow","cache"],"author":{"name":"Mathias Buus Madsen","email":"mathiasbuus@gmail.com"},"bugs":{"url":"https://github.com/mafintosh/thunky/issues"},"homepage":"https://github.com/mafintosh/thunky#readme","license":"MIT","gitHead":"ce46d4fb455af79369cf14b7bf3eb0911bc01500","_id":"thunky@1.0.3","_npmVersion":"6.4.1","_nodeVersion":"10.11.0","_npmUser":{"name":"mafintosh","email":"mathiasbuus@gmail.com"},"dist":{"shasum":"f5df732453407b09191dae73e2a8cc73f381a826","size":2889,"noattachment":false,"key":"/thunky/-/thunky-1.0.3.tgz","tarball":"http://registry.cnpm.dingdandao.com/thunky/download/thunky-1.0.3.tgz"},"maintainers":[{"name":"feross","email":""},{"name":"mafintosh","email":""}],"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/thunky_1.0.3_1539712188066_0.0018386345014538286"},"_hasShrinkwrap":false,"publish_time":1539712188229,"_cnpm_publish_time":1539712188229},"1.0.2":{"name":"thunky","version":"1.0.2","description":"delay the evaluation of a paramless async function and cache the result","main":"index.js","devDependencies":{"standard":"^7.1.2","tape":"^4.6.0"},"repository":{"type":"git","url":"git://github.com/mafintosh/thunky.git"},"scripts":{"test":"standard && tape test.js"},"keywords":["memo","thunk","async","lazy","control","flow","cache"],"author":{"name":"Mathias Buus Madsen","email":"mathiasbuus@gmail.com"},"bugs":{"url":"https://github.com/mafintosh/thunky/issues"},"homepage":"https://github.com/mafintosh/thunky#readme","license":"MIT","gitHead":"7ad8bbfdfbd62b51ea0e34c5235ba0de0db12223","_id":"thunky@1.0.2","_shasum":"a862e018e3fb1ea2ec3fce5d55605cf57f247371","_from":".","_npmVersion":"3.10.10","_nodeVersion":"6.9.5","_npmUser":{"name":"mafintosh","email":"mathiasbuus@gmail.com"},"dist":{"shasum":"a862e018e3fb1ea2ec3fce5d55605cf57f247371","size":2244,"noattachment":false,"key":"/thunky/-/thunky-1.0.2.tgz","tarball":"http://registry.cnpm.dingdandao.com/thunky/download/thunky-1.0.2.tgz"},"maintainers":[{"name":"feross","email":""},{"name":"mafintosh","email":""}],"_npmOperationalInternal":{"host":"packages-18-east.internal.npmjs.com","tmp":"tmp/thunky-1.0.2.tgz_1486860508065_0.44560420024208724"},"directories":{},"publish_time":1486860508724,"_cnpm_publish_time":1486860508724,"_hasShrinkwrap":false},"1.0.1":{"name":"thunky","version":"1.0.1","description":"delay the evaluation of a paramless async function and cache the result","main":"index.js","devDependencies":{"standard":"^7.1.2","tape":"^4.6.0"},"repository":{"type":"git","url":"git://github.com/mafintosh/thunky.git"},"scripts":{"test":"standard && tape test.js"},"keywords":["memo","thunk","async","lazy","control","flow","cache"],"author":{"name":"Mathias Buus Madsen","email":"mathiasbuus@gmail.com"},"bugs":{"url":"https://github.com/mafintosh/thunky/issues"},"homepage":"https://github.com/mafintosh/thunky#readme","license":"MIT","gitHead":"02328e063e740e1b2c6edf8d540bd82577ae87fc","_id":"thunky@1.0.1","_shasum":"3db1525aac0367b67bd2e532d2773e7c40be2e68","_from":".","_npmVersion":"2.15.9","_nodeVersion":"4.4.3","_npmUser":{"name":"mafintosh","email":"mathiasbuus@gmail.com"},"dist":{"shasum":"3db1525aac0367b67bd2e532d2773e7c40be2e68","size":2199,"noattachment":false,"key":"/thunky/-/thunky-1.0.1.tgz","tarball":"http://registry.cnpm.dingdandao.com/thunky/download/thunky-1.0.1.tgz"},"maintainers":[{"name":"feross","email":""},{"name":"mafintosh","email":""}],"_npmOperationalInternal":{"host":"packages-12-west.internal.npmjs.com","tmp":"tmp/thunky-1.0.1.tgz_1468578438552_0.737917662365362"},"directories":{},"publish_time":1468578441115,"_cnpm_publish_time":1468578441115,"_hasShrinkwrap":false},"1.0.0":{"name":"thunky","version":"1.0.0","description":"delay the evaluation of a paramless async function and cache the result","main":"index.js","devDependencies":{"standard":"^7.1.2","tape":"^4.6.0"},"repository":{"type":"git","url":"git://github.com/mafintosh/thunky.git"},"scripts":{"test":"standard && tape test.js"},"keywords":["memo","thunk","async","lazy","control","flow","cache"],"author":{"name":"Mathias Buus Madsen","email":"mathiasbuus@gmail.com"},"bugs":{"url":"https://github.com/mafintosh/thunky/issues"},"homepage":"https://github.com/mafintosh/thunky#readme","license":"MIT","gitHead":"40d0e72ccfea1b2fb11a4706d16d7dc52cdfc9bf","_id":"thunky@1.0.0","_shasum":"48c898447cdbf9686d65f3473bc1489e7cc97184","_from":".","_npmVersion":"2.15.9","_nodeVersion":"4.4.3","_npmUser":{"name":"mafintosh","email":"mathiasbuus@gmail.com"},"dist":{"shasum":"48c898447cdbf9686d65f3473bc1489e7cc97184","size":2186,"noattachment":false,"key":"/thunky/-/thunky-1.0.0.tgz","tarball":"http://registry.cnpm.dingdandao.com/thunky/download/thunky-1.0.0.tgz"},"maintainers":[{"name":"feross","email":""},{"name":"mafintosh","email":""}],"_npmOperationalInternal":{"host":"packages-12-west.internal.npmjs.com","tmp":"tmp/thunky-1.0.0.tgz_1468578380300_0.8760475649032742"},"directories":{},"publish_time":1468578382935,"_cnpm_publish_time":1468578382935,"_hasShrinkwrap":false},"0.1.0":{"name":"thunky","version":"0.1.0","repository":{"type":"git","url":"git://github.com/mafintosh/thunky"},"description":"delay the evaluation of a paramless async function and cache the result","keywords":["memo","thunk","async","lazy","control","flow","cache"],"author":{"name":"Mathias Buus Madsen","email":"mathiasbuus@gmail.com"},"readmeFilename":"README.md","_id":"thunky@0.1.0","dist":{"shasum":"bf30146824e2b6e67b0f2d7a4ac8beb26908684e","size":1449,"noattachment":false,"key":"/thunky/-/thunky-0.1.0.tgz","tarball":"http://registry.cnpm.dingdandao.com/thunky/download/thunky-0.1.0.tgz"},"_from":".","_npmVersion":"1.2.11","_npmUser":{"name":"mafintosh","email":"mathiasbuus@gmail.com"},"maintainers":[{"name":"feross","email":""},{"name":"mafintosh","email":""}],"directories":{},"publish_time":1362594654142,"_cnpm_publish_time":1362594654142,"_hasShrinkwrap":false}},"readme":"# thunky\n\nDelay the evaluation of a paramless async function and cache the result (see [thunk](http://en.wikipedia.org/wiki/Thunk_%28functional_programming%29)).\n\n```\nnpm install thunky\n```\n\n[![build status](http://img.shields.io/travis/mafintosh/thunky.svg?style=flat)](http://travis-ci.org/mafintosh/thunky)\n\n## Example\n\nLet's make a simple function that returns a random number 1 second after it is called for the first time\n\n``` js\nvar thunky = require('thunky')\n\nvar test = thunky(function (callback) { // the inner function should only accept a callback\n  console.log('waiting 1s and returning random number')\n  setTimeout(function () {\n    callback(Math.random())\n  }, 1000)\n})\n\ntest(function (num) {  // inner function is called the first time we call test\n  console.log(num) // prints random number\n})\n\ntest(function (num) {  // subsequent calls waits for the first call to finish and return the same value\n  console.log(num) // prints the same random number as above\n})\n```\n\n## Lazy evaluation\n\nThunky makes it easy to implement a lazy evaluation pattern.\n\n``` js\nvar getDb = thunky(function (callback) {\n  db.open(myConnectionString, callback)\n})\n\nvar queryDb = function (query, callback) {\n  getDb(function (err, db) {\n    if (err) return callback(err)\n    db.query(query, callback)\n  })\n}\n\nqueryDb('some query', function (err, result) { ... } )\n\nqueryDb('some other query', function (err, result) { ... } )\n```\n\nThe first time `getDb` is called it will try do open a connection to the database.\nAny subsequent calls will just wait for the first call to complete and then call your callback.\n\nA nice property of this pattern is that it *easily* allows us to pass any error caused by `getDb` to the `queryDb` callback.\n\n## Error → No caching\n\nIf the thunk callback is called with an `Error` object as the first argument it will not cache the result\n\n``` js\nvar fails = thunky(function (callback) {\n  console.log('returning an error')\n  callback(new Error('bad stuff'))\n})\n\nfails(function (err) { // inner function is called\n  console.log(err)\n});\n\nfails(function (err) { // inner function is called again as it returned an error before\n  console.log(err)\n})\n```\n\n## Promise version\n\nA promise version is available as well\n\n``` js\nvar thunkyp = require('thunky/promise')\n\nvar ready = thunkyp(async function () {\n  // ... do async stuff\n  return 42\n})\n\n// same semantics as the callback version\nawait ready()\n```\n\n## License\n\nMIT\n","_attachments":{},"homepage":"https://github.com/mafintosh/thunky#readme","bugs":{"url":"https://github.com/mafintosh/thunky/issues"},"license":"MIT"}