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

@zeny/safe-async

v1.1.1

Published

A clean, type-safe utility for handling async operations without try-catch blocks. Returns Go-style [error, data] tuples with optional custom logging.

Readme

safe-async

A clean, type-safe utility for handling async operations without try-catch blocks. Returns Go-style [error, data] tuples with optional custom logging.

Why?

Stop writing this:

try {
  const response = await fetch('/api/data');
  const data = await response.json();
  // handle data
} catch (err) {
  // handle error
}

Start writing this:

const [err, data] = await safeAsync(fetch('/api/data').then(r => r.json()));
if (err !== null) {
  // handle error
  return;
}

// handle data

Installation

npm install @zeny/safe-async

Basic Usage

import { safeAsync } from '@zeny/safe-async';

async function example() {
  // Basic usage
  const [err, data] = await safeAsync(fetch('/api/users').then(r => r.json()));
  if (err !== null) {
    console.error('Request failed:', err.message);
    return;
  }

  console.log('Success:', data);
}

API

safeAsync<T, E>(promise, options?)

Parameters:

  • promise: Promise<T> - The promise to execute safely
  • options?: SafeAsyncOptions - Optional configuration

Returns: Promise<[E, null] | [null, T]>

Options:

type SafeAsyncOptions = {
  silent?: boolean; // Default: true (no error logging)
  logger?: (err: Error) => void; // Custom logger function
  processError?: (err: Error) => Error | null; // Transform or suppress errors
};

Option Details

  • silent: Suppresses all error logging. Default: true
  • logger: Custom function to handle error logging. Default: console.error
  • processError: Function to transform errors or suppress logging by returning null

Examples

Basic Error Handling

const [err, user] = await safeAsync(getUserById(123));
if (err !== null) {
  console.log('Failed to get user:', err.message);
  return;
}

console.log('User found:', user.name);

With Custom Logging

const [err, data] = await safeAsync(fetchData(), {
  silent: false,
  logger: err => console.warn('API Error:', err),
});

With Error Processing

// Transform errors and suppress logging for specific cases
const [err, data] = await safeAsync(fetchData(), {
  processError: err => {
    if (err.message.includes('401') === true) {
      return null; // Suppress logging for auth errors
    } else if (err.message.includes('network') === true) {
      return new Error('Network connection failed'); // Transform network errors
    } else return err; // Keep other errors as-is
  },
});

Conditional Error Handling

// Only log non-401 errors
const [err, data] = await safeAsync(fetchData(), {
  processError: err => {
    const isAuthError = err.message.includes('401') || err.message.includes('Unauthorized');
    return isAuthError === true ? null : err;
  },
});

Multiple Operations

async function processUser(userId: string) {
  const [userError, user] = await safeAsync(getUser(userId));
  if (userError !== null) {
    return { error: 'User not found' };
  }

  const [postsError, posts] = await safeAsync(getUserPosts(user.id));
  if (postsError !== null) {
    return { error: 'Failed to load posts' };
  }

  const [updateError] = await safeAsync(updateUserStats(user.id, posts.length));
  if (updateError !== null) {
    return { error: 'Failed to update stats' };
  }

  return { success: true, user, postsCount: posts.length };
}

Custom Wrappers

Fetch Wrapper

For common fetch operations, you can create a simple wrapper:

import { safeAsync, SafeAsyncOptions, SafeAsyncResult } from '@zeny/safe-async';

export const safeFetch = async <T = any>(
  url: string,
  init?: RequestInit,
  options: SafeAsyncOptions = {},
): Promise<SafeAsyncResult<T, Error>> => {
  const fetchPromise = fetch(url, init).then(async response => {
    if (response.ok !== true) {
      throw new Error(`HTTP ${response.status}: ${response.statusText}`);
    }

    return response.json() as Promise<T>;
  });

  return safeAsync(fetchPromise, options);
};

Usage with Fetch Wrapper

const [err, users] = await safeFetch<User[]>('/api/users');
if (err !== null) {
  console.error('Failed to fetch users:', err.message);
  return;
}

console.log('Users list:', users);

TypeScript Support

Full TypeScript support with proper type inference:

// Types are automatically inferred
const [err, data] = await safeAsync(getUser(123));
// error: Error | null
// data: User | null

// Or specify custom error types
const [err, data] = await safeAsync<User, CustomError>(getUser(123));
// error: CustomError | null
// data: User | null

Comparison with Other Libraries

Unlike other safe async libraries, this implementation:

  • Consistent behavior - ALL errors go into the tuple
  • Modern TypeScript - Full type safety and inference
  • Flexible logging - Custom loggers with silent mode
  • Simple API - One function, predictable behavior
  • No magic - No special handling of "native" vs "business" errors

License

MIT