{"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","tarball":"http://registry-npm.ywwl.com/thunky/download/thunky-1.0.0.tgz","integrity":"sha512-yn0c8KcbsbpZPOnrh0JNRKjNidFaty153il1fhF9tO431nRHd3ylVPRjGw0dVmPbbbDEluSoUawimDJgyAN85Q==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIDfNTUU8oGOBxt+QJcE+gEG6o+vWl8U+q8ipPURdZ4jaAiAq8ys7cC4ygyEOlI28kYudxLURb7/HYRG4wllIkdBvUA=="}],"size":2186,"noattachment":false,"key":"/thunky/-/thunky-1.0.0.tgz"},"maintainers":[{"name":"feross","email":"feross@feross.org"},{"name":"mafintosh","email":"mathiasbuus@gmail.com"}],"_npmOperationalInternal":{"host":"packages-12-west.internal.npmjs.com","tmp":"tmp/thunky-1.0.0.tgz_1468578380300_0.8760475649032742"},"directories":{},"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","publish_time":1468578382935,"_cnpm_publish_time":1468578382935,"dist-tags":{"latest":"1.1.0"}}