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 🙏

© 2026 – Pkg Stats / Ryan Hefner

promise-smart-throttle

v0.0.6

Published

Smart throttle utility for Promise

Downloads

26

Readme

Promise Smart Throttle

The Problem

Say you have a Promise-based API like this:

function searchBooks(q) {
  // performs XHR, returns a Promise
  return new Promise(...);
}

Say you have a Search input and you would like the results to be be available as soon as possible.

Normally you would simply attach an input event listener:

input.addEventListener('input', ev => searchBooks(ev.target.value));

But now your server will receive too many requests (one per user's keystroke).

In order to prevent this you would typically use debounce or throttle, but these are only configured using static timeout:

  • set it too high — say goodbye to "as soon as possible" requirement,
  • set it too low — say hello to redundant requests.

Basically, you want the first request to be fired immediately, while all subsequent keystrokes queued until the first request resolves — and then you only want to fire one more request with most recent query.

You could also give a try to concurrency limit libraries like throat, but in this specific case you actually want intermediate requests to be discarded, not queued and executed serially.

The Solution

That's exactly what promise-smart-throttle is for.

You simply wrap your function:

const throttle = require('promise-smart-throttle');

const throttledSearchBooks = throttle(searchBooks);

// ...

input.addEventListener('input', ev => throttledSearchBooks(ev.target.value));

Now the requests will be fired exactly at the rate your server can handle.

Bonus

You can even throw in a second argument:

const throttledSearchBooks = throttle(searchBooks, 100);

This will add a delay after the active promise resolves, in case you also want to slow it down a bit (like you do with regular throttle utils).

Internals

Please observe the exact semantics of how it works (and what resolves with what) by reading test specs.

Specifically,

  • "first call" always resolves ASAP (no delay added)
  • all subsequent calls do not fire until (first call + delay) resolves
  • intermediate calls are basically discarded and resolve to the same result as the last call