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

isotropic-timeout

v0.14.0

Published

Limit the amount of time a callback function will wait to be called

Readme

isotropic-timeout

npm version License

A utility that limits the amount of time a callback function will wait to be called or a promise will wait to be resolved/rejected.

Why Use This?

  • Prevent Indefinite Waiting: Set maximum wait times for asynchronous operations
  • Dual API Support: Works with both callbacks and promises
  • Error Handling: Provides standardized timeout errors
  • Late Response Handling: Option to handle responses that arrive after timeout
  • Simplified Syntax: Clean, concise API for common timeout patterns
  • Zero Dependencies: Lightweight implementation with minimal external requirements

Installation

npm install isotropic-timeout

Usage

With Callback Functions

import _timeout from 'isotropic-timeout';

{
    // Create a time-limited callback function
    const timeLimitedCallback = _timeout(
        5000, // Timeout in milliseconds
        (error, result) => { // Main callback
            if (error) {
                console.error('Operation failed or timed out:', error);

                return;
            }

            console.log('Operation succeeded:', result);
        },
        (error, result) => { // Optional late callback (called if response arrives after timeout)
            console.log('Late response received:', error || result);
        }
    );

    // Pass the time-limited callback to an async operation
    someAsyncOperation(timeLimitedCallback);
}

With Promises

import _timeout from 'isotropic-timeout';

// Wrap a promise with a timeout
const _fetchWithTimeout = async url {
    try {
        const response = await timeout(
            5000, // Timeout in milliseconds
            fetch(url), // Promise to wrap
            (error, result) => { // Optional late callback
                console.log('Late response:', error || result);
            }
        );

        return response.json();
    } catch (error) {
        if (error.name === 'TimeoutError') {
            console.error('Request timed out');
        } else {
            console.error('Request failed:', error);
        }

        throw error;
    }
};

API

timeout(milliseconds, callbackFunctionOrPromise, lateCallbackFunction)

A function that applies a timeout to a callback function or promise.

Parameters

  • milliseconds (Number): The timeout duration in milliseconds
  • callbackFunctionOrPromise (Function|Promise):
    • If a function: The callback to be wrapped with timeout functionality
    • If a promise: The promise to be wrapped with timeout functionality
  • lateCallbackFunction (Function, optional): Function to be called if the original operation completes after timeout

Returns

  • If callbackFunctionOrPromise is a function: Returns a wrapped function that will either call the original callback or trigger a timeout error
  • If callbackFunctionOrPromise is a promise: Returns a new promise that will either resolve/reject with the original promise or reject with a timeout error

Examples

Working with Node.js Callbacks

import _fs from 'node:fs';
import _timeout from 'isotropic-timeout';

// Create a file read operation with a 2-second timeout
_fs.readFile(filepath, 'utf8', _timeout(2000, (error, data) => {
    if (error) {
        if (error.name === 'TimeoutError') {
            console.error('File read timed out');
        } else {
            console.error('File read error:', error);
        }

        return;
    }

    console.log('File contents:', data);
}));

HTTP Request with Timeout

import _https from 'node:https';
import _timeout from 'isotropic-timeout';

const _httpsGet = url => new Promise((resolve, reject) => {
    _https.get(url, response => {
        const chunks = [];

        response.on('data', chunk => {
            chunks.push(chunk);
        });

        response.on('end', () => {
            resolve(chunks.join(''));
        });
    }).on('error', reject);
});

// Add a 3-second timeout to the HTTP request
_timeout(3000, _httpsGet('https://api.example.com/data')).then(data => {
    data = JSON.parse(data);

    console.log('Data received:', data);
}).catch(error => {
    if (error.name === 'TimeoutError') {
        console.error('Request timed out after 3 seconds');
    } else {
        console.error('Request failed:', error);
    }
});

Database Query with Timeout

import _createConnection from 'some-db-library';
import _timeout from 'isotropic-timeout';

const _queryWithTimeout = (db, query, params, timeout = 5000) => {
    return _timeout(
        timeout,
        db.query(query, params),
        (error, results) => {
            // Log late responses for debugging
            if (error) {
                console.log(`Query failed after timeout: ${query}`, error);
            } else {
                console.log(`Query succeeded after timeout: ${query} (${results.length} results)`);
            }
        }
    );
};

{
    const db = _createConnection({ /* connection options */ });

    try {
        const userData = await _queryWithTimeout(db, 'SELECT * FROM users WHERE id = ?', [
            userId
        ]);
    } catch (error) {
        if (error.name === 'TimeoutError') {
            console.error('Database query timed out - server might be overloaded');
        }

        throw error;
    }
}

Error Handling

When a timeout occurs, the utility generates a standardized TimeoutError (an instance of isotropic-error) with:

  • details: Object containing the timeout duration
  • message: 'Timeout after X milliseconds'
  • name: 'TimeoutError'

This makes error handling and identification consistent across your application.

Contributing

Please refer to CONTRIBUTING.md for contribution guidelines.

Issues

If you encounter any issues, please file them at https://github.com/ibi-group/isotropic-timeout/issues