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 🙏

© 2024 – Pkg Stats / Ryan Hefner

@jsep/result

v0.0.4

Published

Error as values

Downloads

3

Readme

Result

NPM Version

A lightweight TypeScript library for handling errors as values.

Motivation

Handling errors in JavaScript/TypeScript can be inconsistent and unclear. Functions may throw exceptions, return null, undefined, or an error object. This inconsistency makes code harder to read and maintain.

Consider this example:

type Todo = {
  id: string;
  title: string;
  completed: boolean;
};

async function getTodoById(id: string): Promise<Todo> {
  const response = await fetch(
    `https://jsonplaceholder.typicode.com/todos/${id}`,
  ); // Can throw an exception
  const json = await response.json(); // Can throw an exception
  return json as Todo; // What if the JSON is not a valid Todo?
}

This function can fail in multiple places, and handling these errors with try/catch blocks can make the code verbose and harder to read. Additionally, the function signature doesn’t indicate that it can fail.

This package provides a set of functions and types to handle errors as values in TypeScript, promoting more explicit error handling and better type safety.

Installation

npm install @jsep/result

Basic Example

import { Result, ok, err, isOk, isErr } from '@jsep/result';
// Example usage
function divide(a: number, b: number): Result<number, string> {
  if (b === 0) {
    return err('Division by zero');
  }
  return ok(a / b);
}

const result = divide(10, 2);

if (isOk(result)) {
  console.log('Result:', result.value);
} else {
  console.error('Error:', result.error);
}

Async Functions

import { Result, ok, err, isOk, isErr } from '@jsep/result';

type Todo = {
  id: string;
  title: string;
  completed: boolean;
};

class FetchError extends Error {}
class ParseError extends Error {}

async function getTodoById(
  id: string,
): Promise<Result<Todo, FetchError | ParseError>> {
  const response = await attemptAsync(() =>
    fetch(`https://jsonplaceholder.typicode.com/todos/${id}`),
  );
  if (isErr(response)) return err(new FetchError('Failed to fetch todo'));

  const json = await attemptAsync(() => response.value.json());
  if (isErr(json)) return err(new ParseError('Failed to parse todo'));

  return ok(json.value as Todo);
}

const todo = await getTodoById('1');

if (isErr(todo)) {
  console.error('Error:', todo.error);
} else {
  console.log('Todo:', todo.value);
}

Alternatives

API

Types

Success<T>

Represents a successful result with a value of type T.

Err<E>

Represents an error result with an error of type E.

Result<T, E>

A union type representing either a successful result (Success<T>) or an error result (Err<E>).

Functions

ok<T>(value: T): Result<T, never>

Creates a successful result with the given value.

err<E>(error: E): Result<never, E>

Creates an error result with the given error.

isOk<T, E>(result: Result<T, E>): result is Result<T, never>

Type guard to check if a result is successful.

isErr<T, E>(result: Result<T, E>): result is Result<never, E>

Type guard to check if a result is an error.

attemptAsync<T>(fn: () => Promise<T>): Promise<Result<T, unknown>>

Wraps an asynchronous function and returns a Result type, catching any errors that occur during execution.

License

MIT