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

memoize-async

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

memoize-async

Async memoization

  • 3.0.3
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
12
decreased by-80.95%
Maintainers
1
Weekly downloads
 
Created
Source

memoize-async

Async memoization

Code Climate Test Coverage Issue Count Circle CI

npm install memoize-async

API

memoize(fn: function [, keyGen: function]): Promise

  • fn: function to evaluate (can be async or sync)
  • keyGen: function used to generate a key from fn params, useful for complex memoization (can be async or sync)

memoize-async expects the global Promise to be defined


Examples

Standard:

import memoize from 'memoize-async'
import { get } from 'highwire'
import { retry } from 'u-promised'

const fn = (url) =>
  retry(3, () =>
    get(url).then((response) =>
      JSON.parse(response.body)))

const memFetch = memoize(fn)

memFetch('http://some.api/things')
  .then((things) => console.log(things)) // must resolve from fn
  .then(() => memFetch('http://some.api/things'))
  .then((things) => console.log(things)) // resolves from cache

With KeyGen:

import memoize from 'memoize-async'
import { get } from 'highwire'
import { retry } from 'u-promised'

const fn = (user) =>
  retry(3, () =>
    get(user.profile).then((response) =>
      JSON.parse(response.body)))

const keyGen = (user) => user.id

const memFetchUserProfile = memoize(fn, keyGen)

const user = {
  id: '1234-5678'
  profile: 'http://some.api/users/1234-5678'
}

memFetchUserProfile(user)
  .then((profile) => console.log(profile)) // sets cache key to '1234-5678'
  .then(() => memFetchUserProfile(user))
  .then((profile) => console.log(profile)) // resolves from cache

Clearing cache:

import memoize from 'memoize-async'
import { get } from 'highwire'
import { retry } from 'u-promised'

const url = 'http://some.api/things'

const fn = (url) =>
  retry(3, () =>
    get(url).then((response) =>
      JSON.parse(response.body)))

const memFetch = memoize(fn)

const CLEAR_TIMER = 1000 * 60 * 5

memFetch(url)
  .then((things) => console.log(things))
  .then(() => setTimeout(() => memFetch.cache.delete(url), CLEAR_TIMER))
  // also
  .then(() => setTimeout(() => memFetch.cache.clear(), CLEAR_TIMER))


Testing

Run npm test. To also generate coverage run npm run test:coverage.

Keywords

FAQs

Package last updated on 01 Jul 2016

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