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

async-dedupe

v1.2.0

Published

Prevent parallel execution of the same async function with identical arguments.

Downloads

5

Readme

async-dedupe

Prevent parallel execution of the same async function with identical arguments.

Key Features

  • Automatic Deduplication: Concurrent calls with identical arguments share a single promise.
  • Zero Caching: Cleans up immediately after completion (no memory leaks).
  • TypeScript Ready: Full type inference for arguments and return values.

How It Works

  • First Call: Executes the function and stores the promise.
  • Parallel Duplicates: Reuses the same promise while the original call is in progress.
  • Completion: Automatically removes the promise (no result caching).

Deepseek hallucination

Install

npm install async-dedupe

Use

import { dedupe } from "async-dedupe"

const generateThumb = dedupe((imagePath: string) => {
  return sharp(imagePath)
    .resize(256, 256, { fit: "outside" })
    .avif()
    .toFile(imagePath + ".avif")
})

// Example: Nuxt.js endpoint.
await defineEventHandler(async (event) => {
  // Concurrent requests with the same path will reuse the in-progress operation.
  return await generateThumbnail(event.query.path)
})

Handling Complex or Multiple Arguments

By default, the function's first argument is used as the deduplication key. For more control, you can provide a custom key function:

const generateThumb = dedupe((
  { source, width, height, target }: {
    source: string
    width: number
    height: number
    target: string
  }) => {
  return sharp(source)
    .resize(width, height, { fit: "outside" })
    .avif()
    .toFile(target)
}, {
  key: ({ target }) => target // Deduplicate based on target path only
})

Accessing Pending Operations

The peek() method allows you to access a currently running promise for specific arguments without creating a new one.

const createBrowser = dedupe(() => puppeteer.launch())

// Handle Ctrl+C
process.on("SIGINT", async () => {
  // Check if a browser is being created
  await createBrowser.peek()?.then(
    browser => browser.close(), // Close browser
    () => {} // Ignore if browser failed to create
  )
  process.exit(0)
})

The peek() method:

  • Takes the same arguments as the original function
  • Returns the pending promise if one exists for those arguments
  • Returns undefined if no promise is running
  • Doesn't create a new promise (unlike calling the function directly)

Waiting for Pending Operations

Alternatively, use settle() to wait for a pending operation and get its settled result (whether fulfilled or rejected).

const createBrowser = dedupe(() => puppeteer.launch())

// Handle Ctrl+C
process.on("SIGINT", async () => {
  // If there is a pending promise for these arguments, wait for it to settle
  const result = await createBrowser.settle()

  // If a promise was running and resolved successfully
  if (result?.status === "fulfilled") {
    await result.value.close()
  }

  process.exit(0)
})

The settle() method resolves to a PromiseSettledResult if a promise was running, or undefined if no promise existed.