Wondering what’s next for npm?Check out our public roadmap! »

    p-memoize
    TypeScript icon, indicating that this package has built-in type declarations

    4.0.1 • Public • Published

    p-memoize Build Status

    Memoize promise-returning & async functions

    Useful for speeding up consecutive function calls by caching the result of calls with identical input.

    By default, only the memoized function's first argument is considered and it only works with primitives. If you need to cache multiple arguments or cache objects by value, have a look at options below.

    Install

    $ npm install p-memoize
    

    Usage

    const pMemoize = require('p-memoize');
    const got = require('got');
     
    const memGot = pMemoize(got, {maxAge: 1000});
     
    (async () => {
        memGot('https://sindresorhus.com');
     
        // This call is cached
        memGot('https://sindresorhus.com');
     
        setTimeout(() => {
            // This call is not cached as the cache has expired
            memGot('https://sindresorhus.com');
        }, 2000);
    })();

    API

    pMemoize(fn, options?)

    Returns a memoized version of the fn function.

    fn

    Type: Function

    Promise-returning or async function to be memoized.

    options

    Type: object

    See the mem options in addition to the below option.

    cachePromiseRejection

    Type: boolean
    Default: false

    Cache rejected promises.

    pMemoize.clear(memoized)

    Clear all cached data of a memoized function.

    Will throw if passed a non-memoized function.

    Related

    Install

    npm i p-memoize

    DownloadsWeekly Downloads

    297,539

    Version

    4.0.1

    License

    MIT

    Unpacked Size

    6.38 kB

    Total Files

    5

    Last publish

    Collaborators

    • avatar
    • avatar