npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

promise-caching

v1.1.5

Published

Minimalist & asynchronous in-memory cache-system working with promises

Downloads

28

Readme

promise-caching

Asynchronous in-memory cache-system working with promises

Typical use-case: you have a function returning a promise with high computing cost.

Execution chart

Here is an execution chart of what is happening when you set 'returnExpired' to false

 [id] |-------------> time
  0   | ====>1
  1   |  ===>1
  2   |   ==>1
  3   |    =>1
  4   |     >1
  5   |      >1
  6   |       >1
  7   |        >1
  8   |         >1
  9   |          ====>2
 10   |           ===>2
 11   |            ==>2
 12   |             =>2
 13   |              >2
 14   |               >2
 15   |                >2
 16   |                 >2

When you set 'returnExpired' to true, retrieving an expired promise won't hang. Instead, it will return the expired promise and regenerate a new result in background.

 [id] |-------------> time
  0   | ====>1
  1   |  ===>1
  2   |   ==>1
  3   |    =>1
  4   |     >1
  5   |      >1
  6   |       >1
  7   |        >1
  8   |         >1
  9   |          >1
 10   |           >1
 11   |            >1
 12   |             >1
 13   |              >1
 14   |               >2
 15   |                >2
 16   |                 >2

To build these execution charts, use

RETURN_EXPIRED=false    npm run execution-chart
RETURN_EXPIRED=true     npm run execution-chart

Usage

Here is an example of how you can use promise-caching.

import {PromiseCaching} from "../src";

// asynchronous sleep function
async function sleep(ms: number) {
    return new Promise(resolve => setTimeout(resolve.bind(this), ms));
}

// the high cost function
async function getRandom() {
    await sleep(1000);
    return Math.random();
}

// your cache instance
let cache: PromiseCaching = new PromiseCaching({ returnExpired: true });


(async () => {

    // cache did not exist. this call takes 1 second
    await cache.get('random', 2000, getRandom);

    // starting from now, cache will be valid 2 seconds

    // this call will be instantaneous
    await cache.get('random', 2000, getRandom);

    // we wait for the cache to expire
    await sleep(2000);

    // the cache has expired now
    await cache.get('random', 2000, getRandom);

    // if 'returnExpired' is true,
    //      the value will be instantaneously returned and the cache
    //      will be generated in the background

    // if 'returnExpired' is false,
    //      the promise will be resolved when the new generation is done

    // you can use whatever key you want for caching
    let key: any = {a: 1};

    await cache.get(key, 2000, getRandom);
    await cache.get(key, 2000, getRandom);
})();