{"id":16531374,"url":"https://github.com/marcbachmann/cached-callback","last_synced_at":"2025-04-04T20:42:45.020Z","repository":{"id":57192524,"uuid":"45494488","full_name":"marcbachmann/cached-callback","owner":"marcbachmann","description":"Resolve all the callbacks registered during an invocation of a function with its value.","archived":false,"fork":false,"pushed_at":"2023-12-15T14:57:55.000Z","size":15,"stargazers_count":2,"open_issues_count":1,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-15T00:24:27.474Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/marcbachmann.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2015-11-03T20:47:14.000Z","updated_at":"2019-03-17T12:05:51.000Z","dependencies_parsed_at":"2023-12-15T16:03:10.045Z","dependency_job_id":null,"html_url":"https://github.com/marcbachmann/cached-callback","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/marcbachmann%2Fcached-callback","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/marcbachmann%2Fcached-callback/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/marcbachmann%2Fcached-callback/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/marcbachmann%2Fcached-callback/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/marcbachmann","download_url":"https://codeload.github.com/marcbachmann/cached-callback/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247249603,"owners_count":20908211,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-11T18:08:40.520Z","updated_at":"2025-04-04T20:42:45.001Z","avatar_url":"https://github.com/marcbachmann.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# cached-callback\n\n[![Greenkeeper badge](https://badges.greenkeeper.io/marcbachmann/cached-callback.svg)](https://greenkeeper.io/)\n\ncached-callback caches arguments returned from an earlier execution and passes them to a callback passed in\n\n\n## Examples\n```js\nvar cachedCallback = require('cached-callback')\n```\n\n### Debounce\nDebounces an invocation with a specific key as long as it's callback is not yet called\n```js\nvar debounced = cachedCallback(function (id, callback) {\n  request('http://example.com/'+id, callback)\n})\n\n// The request only gets triggered once even when you\n// invoke the wrapped method multiple times.\ndebounced('test.html', function (err, data) { console.log(err, data) })\ndebounced('test.html', function (err, data) { console.log(err, data) })\n\n// So this shouldn't result in the same output\nsetTimeout(function () {\n  debounced('test.html', function (err, data) { console.log(err, data) })\n}, 10000)\n```\n\n### Persistent cache\nPermanently caches the result of the callback.\nMultiple invocations result in the same output.\n(Only use this in a controlled environment. This fills an object with the whole response of the callback. So it might lead to a memory leak.)\n```js\nvar cached = cachedCallback(function (id, callback) {\n  request('http://example.com/'+id, callback)\n}, true)\n\ncached('test.html', function (err, data) { console.log(err, data) })\n\n// This is definitely the same response\nsetTimeout(function () {\n  cached('test.html', function (err, data) { console.log(err, data) })\n}, 10000)\n```\n\nThe second `cache` argument is also exposed with `.cache()`\n```js\nvar cached = require('cached-callback').cache()\nvar get = cached(function (id, callback) {\n  request('http://example.com/'+id, callback)\n}\n```\n\n### Custom caching method\nCaches the result with a custom caching method.\nThis example caches the result for 20 seconds.\n```js\nvar cache = {}\nvar setterAndGetter = {\n  get: function get (key) {\n    return cache[key]\n  },\n  set: function set (key, value) {\n    cache[key] = value\n    setTimeout(function () { delete cache[key] }, 20000)\n  }\n}\n\nvar custom = cachedCallback(function (id, callback) {\n  request('http://example.com/'+id, callback)\n}, setterAndGetter)\n```\n\n\n## Why don't I use `async.memoize`?\n\nI ended up writing similar code like this module tons of times and didn't find `async.memoize` when I needed it.\nIMO asyncjs also got too large and could benefit from some modularization.\n\nThe advantage of this module is that you can hook up a custom cache method.\nE.g. a lru cache like https://www.npmjs.com/package/lru-cache\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmarcbachmann%2Fcached-callback","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmarcbachmann%2Fcached-callback","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmarcbachmann%2Fcached-callback/lists"}