{"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","tarball":"http://registry-npm.ywwl.com/thunky/download/thunky-1.0.2.tgz","integrity":"sha512-FW493rSyhf2Wc3apLzxUJlwBVtLvNrL7NnKBHaJkbsAlDcPKjMjTe75cOqSX2kMdPpdIAip38R27Gp6Pea8KGQ==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIBEmao/p5uqlsL1T2Z6uWFYOcTJBzV0PNwiYeDkR3TUgAiBVoldFQ+ho73cXZvvZ2eO227Yw7AsMdzgVJFWz36tZaA=="}],"size":2244,"noattachment":false,"key":"/thunky/-/thunky-1.0.2.tgz"},"maintainers":[{"name":"feross","email":"feross@feross.org"},{"name":"mafintosh","email":"mathiasbuus@gmail.com"}],"_npmOperationalInternal":{"host":"packages-18-east.internal.npmjs.com","tmp":"tmp/thunky-1.0.2.tgz_1486860508065_0.44560420024208724"},"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":1486860508724,"_cnpm_publish_time":1486860508724,"dist-tags":{"latest":"1.1.0"}}