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

reuse-pending-promise

v1.0.0

Published

Re-use pending promises

Downloads

870

Readme

reuse-pending-promise

Reuses a promise by reference until it's settled (resolved or rejected). It helps ensure only one fetch is made at a time--preventing multiple simultaneous fetches for the same data.

It decorates the fn (a thennable) with a cache of pending promises. The cached promise is returned until it has been fulfilled.

reusePendingPromise(fn[, options])

Node.js CI

Install

npm install reuse-pending-promise

Example usage:

import { reusePendingPromise } = from 'reuse-pending-promise'

let callCount = 0
// A promise-returning function
const MyFn = () => {
  callCount++
  return new Promise(resolve => {
    // Wait 10 seconds before resolving
    setTimeout(() => {
      resolve(callCount)
    }, 10 * 1000)
  })
}

// Wrap the promise returning function
const reusedMyFn = reusePendingPromise(MyFn)

Promise
  // Call the reused function 3 times in parallel
  .all([reusedMyFn(), reusedMyFn(), reusedMyFn()])
  .then(() => console.log(`callCount: ${callCount}`))
  // now that the first promise has resolved...
  .then(reusedMyFn)
  .then(() => console.log(`callCount: ${callCount}`))

// 10 seconds later, the initial three calls resolve but the `myFn` is only invoked once:
// // callCount: 1

// 20 seconds later, the last call will resolve, having invoked `myFn` a second time.
// // callCount: 2

Using getCacheKey option for variations

The getCacheKey option can be used to cache variations based on the fn's arguments, similar to the resolver argument in lodash.memoize. By default the first argument is used as the cache key.

Here's an example where promises are re-used only if the lang and country arguments match:

const fetchData = (lang, country) => fetch(`http://example.com/${country}/${lang}`)
const getCacheKey = (lang, country) => `${lang}${country}`

const reusedFetchData = reusePendingPromise(fetchData, { getCacheKey })
// promise1 and promise2 will share the same promise returned by fetch,
// since they share a cache key.
const promise1 = reusedFetchData('en', 'canada')
const promise2 = reusedFetchData('en', 'canada')
// promise1 === promise2

// promise3 will be a different cache key, thus will be a new promise for
// a second second fetch call.
const promise3 = reusedFetchData('fr', 'canada')
// promise3 !== promise1

Additional examples are in the unit test

Contributing

Fork and create a PR.

License

This project is MIT licensed.