{"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":{"integrity":"sha512-YwT8pjmNcAXBZqrubu22P4FYsh2D4dxRmnWBOL8Jk8bUcRUtc5326kx32tuTmFDAZtLOGEVNl8POAR8j896Iow==","shasum":"f5df732453407b09191dae73e2a8cc73f381a826","tarball":"http://registry-npm.ywwl.com/thunky/download/thunky-1.0.3.tgz","fileCount":6,"unpackedSize":7165,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJbxiS9CRA9TVsSAnZWagAA8DsQAKIU9+suySLueSdVZcn1\n/OMQQPWU6PyfDjI6vZq2ECE7PYB9kaOTj45bsVoUZOts9cI0YAqzYFEJK2Qn\n0ZTT+X1bOsmNaNsg3Llv2VWqwBl0uLQcWordK+HsgdUW/Pj7to2D2gfOeIDK\n6CdhGYxD7WLBcS6eJ/KqBGZvMgfToX/w+9GfTjPzU0IxU+1XNCugs8QSrS+z\nYObEgpn1DYjlIarubypRSPd5la+hx/yGQlfpq9barM1Y4V9p87Yt8UL5qw7p\n7ZZIMiR/B78G7s8lrrmzgYSXTui740yjmC5SAXRzHuuGN3WjnFLQSv2G1uKY\n8ZPsUuqo2a7Fj6LFjZjlEcoPS0Lwft0HYRrJ3r1hCpEgDuLoOxjm9z6adstu\nqlvw5LDTWyanE4jM8bndZvf+ay08+kbFUdRmXbeIriHpZD8YdeL+IQ3pl5t/\n4mfA+tDVSGf/FF5tLY5JBSPAelRlJhUUqWGKQ/G/qNhIN+JdioqBTzy3XtZo\nFx3k/VlXmIYlaYvYdvNeAJIGEkyk+g8TXaupbzwLMuh5P7X+jCTsaa3yb4FJ\nmRFrwXfATuQeHMDKrtUHMjlecxF9M91bCRxCJ2AdP9Msq08zF8qHhOiz8bH0\n9f+o0W8zXuLNuWD4Zb8HjbnnOptZzh8m6s3nhCu5xbHT259LZ8gV9ogbpwp5\nG2s6\r\n=VdWZ\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQCqcRArz7Akjiey+7xN989P7a/ceeqThdtDt5ecugI7GAIhAJ2qVuY4WmJvzPZt7XB1+9jSvE6pzdvGk2rkYYnMGNGH"}],"size":2889,"noattachment":false,"key":"/thunky/-/thunky-1.0.3.tgz"},"maintainers":[{"name":"feross","email":"feross@feross.org"},{"name":"mafintosh","email":"mathiasbuus@gmail.com"}],"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/thunky_1.0.3_1539712188066_0.0018386345014538286"},"_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","publish_time":1539712188229,"_cnpm_publish_time":1539712188229,"dist-tags":{"latest":"1.1.0"}}