Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@green-code/memoize-async

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@green-code/memoize-async

Memoize the result of any function, even an asynchronous one

  • 1.0.1
  • unpublished
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

Memoize Async

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.

Installation

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.

Usage

Initialise With Pre-loaded Data

const memoizeAsync = require("@green-code/memoize-async")({foo: "bar", biz: "baz"});

Get and Set

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!;

Get All

console.log(memoizeAysnc.getAll()); // {'["hello","AsyncFunction","world"]': 'Hello world!','["hello","AsyncFunction","Joe"]': 'Hello Joe!'}

Delete By Key

memoizeAysnc.deleteByKey('["hello","AsyncFunction","Joe"]');
console.log(memoizeAysnc.getAll()); // {'["hello","AsyncFunction","world"]': 'Hello world!'}

Clear All

memoizeAysnc.clear();
console.log(memoizeAysnc.getAll()); // {}

Listening For Events

const EventEmitter = require("events");
const eventEmitter = new EventEmitter();

eventEmitter.on("memoizeAsync.operation.complete", ({key, value}) => {
    // do something
});
Event Names
memoizeAsync.operation.start

When the function is invoked but not yet completed.

memoizeAsync.operation.success.unsaved

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.

memoizeAsync.operation.success.saved

The function was called successfully and the value was saved to memory.

memoizeAsync.operation.fail

The function returned an error.

memoizeAsync.operation.complete

Always fires whether the value was saved or not.

memoizeAsync.get

Fires when getting an item previously memoized in memory.

memoizeAsync.getall

Fires when getting all items memoized in memory.

memoizeAsync.delete

Fires when deleting a memoized item from memory.

memoizeAsync.queue.add

When a function call is added to the queue. This is ensures the same function with the same arguments isn't called twice.

memoizeAsync.queue.remove

When a function is removed fromm the queue.

memoizeAsync.queue.clear

When the queue is cleared.

Keywords

FAQs

Package last updated on 06 Dec 2021

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc