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 🙏

© 2025 – Pkg Stats / Ryan Hefner

async-wrapus

v1.1.0

Published

An elegant wrapper for async functions that eliminates the need for try-catch blocks and makes error handling more straightforward.

Downloads

27

Readme

async-wrapus

An elegant wrapper for async functions that eliminates the need for try-catch blocks and makes error handling more straightforward.

NPM JavaScript Style Guide Badges Badges Badges Badges Badges Badges

Description

async-wrapus is a lightweight utility that simplifies error handling in asynchronous operations. It transforms the traditional try-catch pattern into a more functional approach, inspired by Go's error handling pattern.

Why use async-wrapus?

  • Clean Code: Eliminates nested try-catch blocks that can make code harder to read
  • Type Safety: Full TypeScript support with proper type inference
  • Predictable Error Handling: Consistent error handling pattern across your application
  • Zero Dependencies: Lightweight and simple implementation
  • Universal: Works in Node.js and browser environments

Install

npm install --save async-wrapus
yarn add async-wrapus

Usage

Basic Example

import asyncWrap from 'async-wrapus';

const fetchUserData = async (userId: string) => {
  const [error, data] = await asyncWrap(api.fetchUser(userId));
  
  if (error) {
    console.error('Failed to fetch user:', error.message);
    return null;
  }
  
  return data;
};

Advanced Usage

import asyncWrap from 'async-wrapus';

const complexOperation = async () => {
  // Multiple async operations
  const [dbError, dbResult] = await asyncWrap(database.query());
  if (dbError) {
    // Handle database error
    return [dbError, null];
  }

  // Chain operations
  const [apiError, apiResult] = await asyncWrap(api.process(dbResult));
  if (apiError) {
    // Handle API error
    return [apiError, null];
  }

  return [null, { db: dbResult, api: apiResult }];
};

// Using with Promise.all
const batchOperation = async () => {
  
  const operations = [
    Promise.resolve("test"),
    Promise.reject("reject"),
    Promise.resolve("test"),
  ];
  
  const [error, results] = await asyncWrap(Promise.all(operations));
  
  // Check if any operation failed
  if (error) {
    // Handle errors
  }
};


// Using with Promise.all
const batchOperation = async () => {
  const operations = [
    api.operation1(),
    api.operation2(),
    api.operation3()
  ].map(asyncWrap);
  
  const results = await Promise.all(operations);
  
  // Check if any operation failed
  const hasError = results.some(([error]) => error !== null);
  if (hasError) {
    // Handle errors
  }

Return Type

The wrapper returns a tuple (array) with two elements:

  • First element: Error | null - Error object if operation failed, null if successful
  • Second element: T | null - Result of the operation if successful, null if failed
type AsyncWrapResult<T> = Promise<[Error | null, T | null]>;

Error Handling Examples

const [error, result] = await asyncWrap(someAsyncOperation());

// Pattern 1: Early return
if (error) {
  return handleError(error);
}

// Pattern 2: Error logging
if (error) {
  logger.error('Operation failed:', error);
  return fallbackValue;
}

// Pattern 3: Conditional logic
if (error) {
  if (error instanceof NetworkError) {
    // Handle network errors
  } else if (error instanceof ValidationError) {
    // Handle validation errors
  }
}

License

MIT © kolengri