Code Monkey home page Code Monkey logo

async-memoize-last's Introduction

async-memoize-last

Memoize your last most api calls.

Installation

npm install -s async-memoize-last
yarn add async-memoize-last

How Does it Work?

It memoizes and compares to your last call, if the parameters are exactly the same then it will resolve the promise with the previous response. This reduces load time (not having to wait for an api response) and data usage.

This if to be used for making asynchronous requests that, if given the same parameters in a reasonable amount of time, will return the same response. This (obviously) will mess up api calls that return different results when called with the same params at different times. Use at your own descretion.

Use Cases

Say theres an api call that you would need to perform multiple times that return the same result given the same parameters, like a distance calculation to an api that returns the distance between two points on the earth. You want to save up on costs so you don't want to perform two calls that will return the exact same result.

Api

// Basic Usage
const memoizedCall = asyncMemoize(call);

// With custom equality function
const equalityFunction = (a, b) => {
    return a - b
};
const memoizedCallWithCustomEqual = asyncMemoize(call, equalityFunction);

Options

Equality Function

    equalityFunction: (prevArgs, newArgs) => prevArgs[0] === newArgs[0] // args array

A custom equality function to compare the parameters of the function. Default is a deep equality function.

By default, this uses the deep equality function provided by fast-deep-equal. You are free to supply your own equality function to the second parameter of the asyncMemoizeLast function.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.