mirror of
https://github.com/KevinMidboe/zoff.git
synced 2025-10-29 18:00:23 +00:00
45 lines
3.1 KiB
JSON
Executable File
45 lines
3.1 KiB
JSON
Executable File
{
|
|
"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"
|
|
},
|
|
"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\tnpm install 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\tconsole.log('waiting 1s and returning random number');\n\tsetTimeout(function() {\n\t\tcallback(Math.random());\n\t}, 1000);\n});\n\ntest(function(num) { // inner function is called the first time we call test\n\tconsole.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\tconsole.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\tdb.open(myConnectionString, callback);\n});\n\nvar queryDb = function(query, callback) {\n\tgetDb(function(err, db) {\n\t\tif (err) return callback(err);\n\t\tdb.query(query, callback);\n\t});\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\tconsole.log('returning an error');\n\tcallback(new Error('bad stuff'));\n});\n\nfails(function(err) { // inner function is called\n\tconsole.log(err);\n});\n\nfails(function(err) { // inner function is called again as it returned an error before\n\tconsole.log(err);\n});\n```\n\n## License\n\nMIT",
|
|
"readmeFilename": "README.md",
|
|
"_id": "thunky@0.1.0",
|
|
"dist": {
|
|
"shasum": "bf30146824e2b6e67b0f2d7a4ac8beb26908684e",
|
|
"tarball": "http://registry.npmjs.org/thunky/-/thunky-0.1.0.tgz"
|
|
},
|
|
"_from": "thunky@~0.1.0",
|
|
"_npmVersion": "1.2.11",
|
|
"_npmUser": {
|
|
"name": "mafintosh",
|
|
"email": "mathiasbuus@gmail.com"
|
|
},
|
|
"maintainers": [
|
|
{
|
|
"name": "mafintosh",
|
|
"email": "mathiasbuus@gmail.com"
|
|
}
|
|
],
|
|
"directories": {},
|
|
"_shasum": "bf30146824e2b6e67b0f2d7a4ac8beb26908684e",
|
|
"_resolved": "https://registry.npmjs.org/thunky/-/thunky-0.1.0.tgz"
|
|
}
|