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-tuple

v1.2.0

Published

A utility function for handling promises with Go-like error handling pattern

Readme

Promise Tuple

A lightweight utility function for handling promises with Go-like error handling pattern. Instead of using try/catch blocks, promiseTuple returns a tuple containing either an error or a result.

Features

  • 🎯 Simple and intuitive error handling
  • 📦 TypeScript support with full type safety
  • 🔄 Go-style error handling pattern
  • ⚡ Zero dependencies
  • 🌐 Works in Node.js and browsers

Installation

npm install promise-tuple

or with yarn:

yarn add promise-tuple

or with pnpm:

pnpm add promise-tuple

How to Use

Import

ES Module:

import promiseTuple from 'promise-tuple';

CommonJS:

const promiseTuple = require('promise-tuple');

Basic Usage

import promiseTuple from 'promise-tuple';

// Example with a fetch request
const [error, data] = await promiseTuple(fetch('/api/data'));

if (error) {
    console.error('Error:', error);
} else {
    console.log('Data:', data);
}

With Custom Types

You can specify the type of the resolved value and error:

import promiseTuple from 'promise-tuple';

interface User {
    id: number;
    name: string;
}

const [error, user] = await promiseTuple<User>(
    fetch('/api/user').then((res) => res.json()),
);

if (error) {
    console.error('Failed to fetch user:', error);
} else {
    console.log('User:', user?.name);
}

In async/await Functions

async function getUser(userId: string) {
    const [error, response] = await promiseTuple(fetch(`/api/users/${userId}`));

    if (error) {
        throw new Error(`Failed to fetch user: ${error}`);
    }

    const [parseError, user] = await promiseTuple(response.json());

    if (parseError) {
        throw new Error(`Failed to parse user data: ${parseError}`);
    }

    return user;
}

With Custom Error Types

import promiseTuple from 'promise-tuple';

interface CustomError {
    code: string;
    message: string;
}

const [error, data] = await promiseTuple<string, CustomError>(
    someAsyncOperation(),
);

With Callbacks

You can optionally provide success and failure callbacks:

import promiseTuple from 'promise-tuple';

const [error, data] = await promiseTuple(
    fetch('/api/data'),
    () => console.log('Request succeeded!'),
    () => console.log('Request failed!'),
);

Only the appropriate callback will be executed:

import promiseTuple from 'promise-tuple';

const [error, data] = await promiseTuple(
    someAsyncOperation(),
    () => {
        // This runs on success
        console.log('Operation completed successfully');
        sendAnalytics('success');
    },
    () => {
        // This runs on failure
        console.error('Operation failed');
        sendAnalytics('failure');
    },
);

API

promiseTuple<R, E>(promise, successCallback?, failureCallback?)

Handles a promise and returns a tuple with error and result.

Parameters:

  • promise: Promise<R> - The promise to handle
  • successCallback?: () => void - Optional callback executed when the promise resolves
  • failureCallback?: () => void - Optional callback executed when the promise rejects

Returns:

  • Promise<[E | undefined, R | undefined]> - A promise that resolves to a tuple containing either an error or a result

Type Parameters:

  • R (default: unknown) - The type of the resolved value
  • E (default: unknown) - The type of the error

Behavior:

  • If the promise resolves: returns [undefined, result] and executes successCallback if provided
  • If the promise rejects: returns [error, undefined] and executes failureCallback if provided

Examples

Database Query

import promiseTuple from 'promise-tuple';
import db from './database';

async function getUserById(id: number) {
    const [error, user] = await promiseTuple(db.users.findById(id));

    if (error) {
        return { success: false, message: 'User not found' };
    }

    return { success: true, user };
}

File Operations

import promiseTuple from 'promise-tuple';
import fs from 'fs/promises';

async function readConfigFile(path: string) {
    const [error, content] = await promiseTuple(fs.readFile(path, 'utf-8'));

    if (error) {
        console.error('Failed to read config:', error);
        return null;
    }

    return JSON.parse(content);
}

With Callbacks for Side Effects

import promiseTuple from 'promise-tuple';
import db from './database';

async function getUserById(id: number) {
    const [error, user] = await promiseTuple(
        db.users.findById(id),
        () => {
            // Track successful database query
            console.log(`User ${id} retrieved successfully`);
            metrics.incrementCounter('db.user.success');
        },
        () => {
            // Track failed database query
            console.error(`Failed to retrieve user ${id}`);
            metrics.incrementCounter('db.user.failure');
        },
    );

    if (error) {
        return { success: false, message: 'User not found' };
    }

    return { success: true, user };
}

License

ISC