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

xflight

v2.0.2

Published

Handle inflight promise to avoid async duplication

Readme

xflight

Avoid redundant async calls by sharing inflight promises for the same key.

Description

xflight is a lightweight Node.js utility that manages inflight promises by key. It ensures that only one asynchronous operation per key is running at a time, returning the same promise for concurrent requests. This is useful for avoiding duplicate network or resource-intensive calls.

Features

  • Prevents duplicate async operations for the same key
  • Tracks start and check times for inflight items
  • Cleans up after promise resolution or rejection
  • 100% test coverage

Installation

npm install xflight

Requirements

This package requires Node.js >= 20.

Usage

const Xflight = require("xflight");
const xfl = new Xflight();

function fetchData(url) {
  return xfl.promise(url, () => fetch(url));
}

// Multiple calls with the same URL will share the same promise if still pending
fetchData("https://api.example.com/data").then(console.log);
fetchData("https://api.example.com/data").then(console.log);

Usage in ESM and CommonJS

ESM (ECMAScript Modules)

import Inflight from "xflight";

const inflight = new Inflight();
const key = "resource-1";

// Deduplicate async calls
const resultPromise = inflight.promise(key, () => fetch("https://api.example.com/data"));

CommonJS

const Inflight = require("xflight").default;

const inflight = new Inflight();
// ... use as shown in examples above

API

new Xflight([PromiseImpl])

  • PromiseImpl (optional): Custom Promise implementation (e.g., Bluebird, Aveazul, or native Promise).

Note: By default, the constructor will try to use Bluebird or Aveazul as the Promise implementation if they are available. If you want to always use the native Promise and skip these checks, pass the global Promise as the argument:

const inflight = new Inflight(Promise);

Methods

promise(key, promiseFactory)

  • key: Unique identifier for the inflight operation.
  • promiseFactory: Function that returns a promise.
  • Returns: The promise from promiseFactory, or the existing inflight promise for the key.

add(key, value, [now])

  • Manually add an inflight item. value should be a promise.

get(key)

  • Get the current inflight promise for a key, or undefined.

remove(key)

  • Remove an inflight item by key.

isEmpty

  • Boolean: true if no inflight items.

count

  • Number of inflight items.

Timing Methods

  • getStartTime(key): Get start time (ms since epoch) for a key.
  • time(key, [now]) / elapseTime(key, [now]): Elapsed time since start.
  • getCheckTime(key): Get last check time for a key.
  • lastCheckTime(key, [now]) / elapseCheckTime(key, [now]): Elapsed time since last check.
  • resetCheckTime(key, [now]): Reset last check time to now.

Testing

To run tests:

npm test

Test coverage is enforced at 100% using nyc.

Examples

Basic Usage with promise

import Inflight from "xflight";

const inflight = new Inflight();
const key = "resource-1";

// Deduplicate async calls
const resultPromise = inflight.promise(key, () => fetch("https://api.example.com/data"));
// or, for clarity:
const resultPromise2 = inflight.promise(key, function promiseFactory() {
  return fetch("https://api.example.com/data");
});

Manually Add and Get an Inflight Promise

const promise = new Promise((resolve) => setTimeout(() => resolve("done"), 100));
inflight.add("manual-key", promise);
const inflightPromise = inflight.get("manual-key"); // Promise<string> | undefined

Remove an Inflight Item

inflight.remove("manual-key");

Check if Inflight is Empty and Get Count

console.log(inflight.isEmpty); // true or false
console.log(inflight.count);   // number of inflight items

Timing Methods

inflight.add("timed-key", new Promise(() => {}));
const start = inflight.getStartTime("timed-key");
const elapsed = inflight.time("timed-key");
const elapsedAlias = inflight.elapseTime("timed-key");

Check Time Methods

const check = inflight.getCheckTime("timed-key");
const sinceLastCheck = inflight.lastCheckTime("timed-key");
const sinceLastCheckAlias = inflight.elapseCheckTime("timed-key");

Reset Check Time

// Reset for a specific key
inflight.resetCheckTime("timed-key");
// Reset for all inflight items
inflight.resetCheckTime();

License

Apache-2.0


© Joel Chen