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 🙏

© 2024 – Pkg Stats / Ryan Hefner

remember-promise

v2.0.3

Published

Remembering promises that were made!

Downloads

50

Readme

remember-promise

bundle size codecov JSR score JSR npm license

A simple utility to remember promises that were made! It is greatly inspired by the p-memoize utility but with additional built-in features and changes such as:

Installation

remember-promise is available on both npm and JSR.

To use from npm, install the remember-promise package and then import into a module:

import { rememberPromise } from "remember-promise";

To use from JSR, install the @reda/remember-promise package and then import into a module:

import { rememberPromise } from "@reda/remember-promise";

Usage

import { rememberPromise } from "remember-promise";

const getRedditFeed = rememberPromise(
  (subreddit) =>
    fetch(`https://www.reddit.com/r/${subreddit}.json`).then((res) =>
      res.json()
    ),
  {
    ttl: 300_000, // 5 minutes before the result must be revalidated again
    /* see below for a full list of available options */
  },
);

const firstResult = await getRedditFeed("all");
const secondResult = await getRedditFeed("all"); // this call is cached

Options

| Name | Description | | :------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | ttl | Configures how long in milliseconds the cached result should be used before needing to be revalidated. Additionally, setting this value to zero or a negative number will disable caching. NOTE: the actual revalidation of the cached result is done slightly before expiry by default. This can be adjusted using the xfetchBeta option. By default this is Infinity so the cached result will be used indefinitely. | | allowStale | This enables stale-while-revalidate behavior where an expired result can still be used while waiting for it to be updated in the background asynchronously. By default this is set to true so the behavior is enabled. | | cache | This is where cached results will be stored. It can be anything you want such as lru-cache or a redis backed cache as long as it implements a get and set method defined in the RememberPromiseCache type. If you would like to disable caching and only deduplicate identical concurrent calls instead then set this to false. When this is set to false, the onCacheUpdateError and shouldIgnoreResult options will be never be used. By default this is a new instance of Map. | | getCacheKey | Identical behavior to the cacheKey option in p-memoize except that it's allowed to return a promise. It should return what the cache key is based on the parameters of the given function. By default this will serialize all arguments using JSON.stringify. | | onCacheUpdateError | Use this to catch errors when attempting to update the cache or if shouldIgnoreResult throws an error. By default this is undefined which means any errors will be rethrown as an unhandled promise rejection. | | shouldIgnoreResult | Determines whether the returned result should be added to the cache. By default this is undefined which means it will always use the returned result for caching. | | xfetchBeta | This is the beta value used in optimal probabilistic cache stampede prevention where values more than 1 favors earlier revalidation while values less than 1 favors later revalidation. By default this is set to 1 so the revalidation of a cached result will happen at a random time slightly before expiry. If you wish to opt-out of this behavior, then set this value to 0. |