
Product
Rust Support Now in Beta
Socket's Rust support is moving to Beta: all users can scan Cargo projects and generate SBOMs, including Cargo.toml-only crates, with Rust-aware supply chain checks.
@green-code/memoize-async
Advanced tools
Memoize the result of any function, even an asynchronous one
Memoize the value of any promise or synchronous function.
Generator functions are not supported.
Memoizing the same function with the same arguments while the async operation is still running will result in an undefined result, but you may listen for the result of the event memoizeAsync.operation.complete
from wherever it's called.
npm install
If you intend to use this in an non-Node based environment, for example a browser, you must also run npm add events
.
const memoizeAsync = require("@green-code/memoize-async")({foo: "bar", biz: "baz"});
const hello = async (string) => new Promise(resolve => resolve(`Hello ${string}!`));
// Arguments must be wrapped in an array because they will be passed to your function using the spread operator
memoizeAsync.getAndSet(hello, ["world"]).then(console.log); // Hello world!;
memoizeAsync.getAndSet(hello, ["Joe"]).then(console.log); // Hello Joe!;
console.log(memoizeAysnc.getAll()); // {'["hello","AsyncFunction","world"]': 'Hello world!','["hello","AsyncFunction","Joe"]': 'Hello Joe!'}
memoizeAysnc.deleteByKey('["hello","AsyncFunction","Joe"]');
console.log(memoizeAysnc.getAll()); // {'["hello","AsyncFunction","world"]': 'Hello world!'}
memoizeAysnc.clear();
console.log(memoizeAysnc.getAll()); // {}
const EventEmitter = require("events");
const eventEmitter = new EventEmitter();
eventEmitter.on("memoizeAsync.operation.complete", ({key, value}) => {
// do something
});
When the function is invoked but not yet completed.
The function was called successfully and the value was not saved to memory because it is either undefined, null or an empty string. You might want to check the arguments you pass to the function or check the function itself.
The function was called successfully and the value was saved to memory.
The function returned an error.
Always fires whether the value was saved or not.
Fires when getting an item previously memoized in memory.
Fires when getting all items memoized in memory.
Fires when deleting a memoized item from memory.
When a function call is added to the queue. This is ensures the same function with the same arguments isn't called twice.
When a function is removed fromm the queue.
When the queue is cleared.
FAQs
Memoize the result of any function, even an asynchronous one
We found that @green-code/memoize-async demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Socket's Rust support is moving to Beta: all users can scan Cargo projects and generate SBOMs, including Cargo.toml-only crates, with Rust-aware supply chain checks.
Product
Socket Fix 2.0 brings targeted CVE remediation, smarter upgrade planning, and broader ecosystem support to help developers get to zero alerts.
Security News
Socket CEO Feross Aboukhadijeh joins Risky Business Weekly to unpack recent npm phishing attacks, their limited impact, and the risks if attackers get smarter.