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

    1.1.1 • Public • Published

    memoizeConcurrent

    memoize async functions such that concurrent calls return the same promise

    Installation

    npm i --save memoize-concurrent

    Usage

    Supports both ESM and CommonJS

    // esm
    import memo from 'memoize-concurrent'
    // commonjs
    const memo = require('memoize-concurrent').default

    Basic Example

    Example showing that concurrent calls return the same promise and asyncronous calls invoke the passed function

    import memo from 'memoize-concurrent'
    
    const memoizedFetch = memo(fetch)
    
    const promise1 = memoizedAdd('http://localhost') // hits server
    const promise2 = memoizedAdd('http://localhost') // hits cache
    console.log(promise1 === promise2) // true
    await promise1
    const promise3 = memoizedAdd('http://localhost') // hits server
    console.log(promise1 === promise3) // false

    Uses mem for Memoization

    memoizeConcurrent uses mem under the hood and supports the same options

    import memo from 'memoize-concurrent'
    
    const memoizedFetch = memo(fetch, {
      maxAge: 10, // maxAge of value in cache
      cacheKey: (args) => args[0], // function to compute cache key
      cache: new Map(), // provide your own cache
    })

    License

    MIT

    Install

    npm i memoize-concurrent

    DownloadsWeekly Downloads

    2

    Version

    1.1.1

    License

    MIT

    Unpacked Size

    29.1 kB

    Total Files

    15

    Last publish

    Collaborators

    • tjmehta