{"_id":"memolite","_rev":"481020","name":"memolite","description":"extremly simple memoizing for async functions","dist-tags":{"latest":"0.1.0"},"maintainers":[{"name":"mafintosh","email":""}],"time":{"modified":"2021-08-05T09:37:09.000Z","created":"2013-02-28T00:16:24.114Z","0.1.0":"2013-02-28T00:16:24.114Z"},"users":{},"author":{"name":"Mathias Buus Madsen","email":"mathiasbuus@gmail.com"},"repository":{"type":"git","url":"git://github.com/mafintosh/memolite"},"versions":{"0.1.0":{"name":"memolite","version":"0.1.0","repository":{"type":"git","url":"git://github.com/mafintosh/memolite"},"description":"extremly simple memoizing for async functions","keywords":["memo","memoize","async","simple"],"author":{"name":"Mathias Buus Madsen","email":"mathiasbuus@gmail.com"},"readmeFilename":"README.md","_id":"memolite@0.1.0","dist":{"shasum":"df9dd5132029676224bbd3c090e0c5952abf4afd","size":1328,"noattachment":false,"key":"/memolite/-/memolite-0.1.0.tgz","tarball":"http://registry.cnpm.dingdandao.com/memolite/download/memolite-0.1.0.tgz"},"_from":".","_npmVersion":"1.2.11","_npmUser":{"name":"mafintosh","email":"mathiasbuus@gmail.com"},"maintainers":[{"name":"mafintosh","email":""}],"directories":{},"publish_time":1362010584114,"_cnpm_publish_time":1362010584114,"_hasShrinkwrap":false}},"readme":"# memolite\n\nExtremely simple memoizing for async functions.\nIt is available through npm\n\n\tnpm install memolite\n\n## Usage\n\nmemolite memoizes async functions on the form `function(callback) { ... }`\n\n``` js\nvar memolite = require('memolite');\nvar fs = require('fs');\n\nvar random = memolite(function(callback) {\n\tconsole.log('I am only called once');\n\tsetTimeout(function() {\n\t\tcallback(Math.random());\n\t}, 1000);\n});\n\nrandom(function(num) {\n\tconsole.log(num); // prints a random number after 1s\n});\n\nrandom(function(num) {\n\tconsole.log(num); // prints the same number as above\n});\n```\n\nIf you wanted to do a file reader that only read files once and remembered the result you would do\n\n``` js\nvar memolite = require('memolite');\nvar fs = require('fs');\n\nvar fileReader = function(filename) {\n\treturn memolite(function(callback) {\n\t\tconsole.log('reading '+filename);\n\t\tfs.readFile(filename, callback);\n\t});\n};\n\nvar readReadme = fileReader('README.md');\n\nreadReadme(function(err, data) { // prints 'reading README.md'\n\tconsole.log('README is '+data);\n});\n\nreadReadme(function(err, data) { // memolite will wait for the read to finish and return the data\n\tconsole.log('README is '+data);\n});\n```\n\nIf an error is passed to the callback then memolite will not remember the result\n\n``` js\nvar run = memolite(function(callback) {\n\tconsole.log('running');\n\tcallback(new Error('bad things'));\n});\n\nrun(function(err) { // 'running' is printed\n\tconsole.log(err);\n});\n\nrun(function(err) { // 'running' is printed again\n\tconsole.log(err);\n});\n```\n\nA great usecase for memolite is doing lazy operations.\n\n``` js\nvar makeFolder = memolite(function(callback) {\n\tfs.mkdir('/tmp/my-folder', callback);\n});\n\nvar writeFile = function(filename, data, callback) {\n\tmakeFolder(function(err) {\n\t\tif (err) return callback(err);\n\t\tfs.writeFile('/tmp/my-folder/'+filename, data, callback);\n\t});\n};\n```\n\nCalling `writeFile` multiple times would result in `fs.mkdir(...)` being called only once (if there are no errors).\n\n## License\n\nMIT","_attachments":{},"readmeFilename":"README.md"}