Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

neverthrow

Package Overview
Dependencies
Maintainers
1
Versions
62
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

neverthrow

Stop throwing errors, and instead return Results!

  • 8.1.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
799K
increased by20.57%
Maintainers
1
Weekly downloads
 
Created

What is neverthrow?

The neverthrow npm package provides a functional way to handle errors in TypeScript and JavaScript. It introduces the Result and Option types, which help in managing success and failure cases without using exceptions.

What are neverthrow's main functionalities?

Result Type

The Result type is used to represent either a success (ok) or a failure (err). This example demonstrates a division function that returns a Result type, handling division by zero as an error.

const { ok, err, Result } = require('neverthrow');

function divide(a, b) {
  if (b === 0) {
    return err(new Error('Division by zero'));
  }
  return ok(a / b);
}

const result = divide(4, 2);
result.match({
  ok: value => console.log('Result:', value),
  err: error => console.error('Error:', error.message)
});

Option Type

The Option type is used to represent an optional value that may or may not be present. This example shows a function that looks up a user by ID and returns an Option type.

const { some, none, Option } = require('neverthrow');

function findUserById(id) {
  const users = { 1: 'Alice', 2: 'Bob' };
  return users[id] ? some(users[id]) : none();
}

const user = findUserById(1);
user.match({
  some: value => console.log('User found:', value),
  none: () => console.log('User not found')
});

Chaining Operations

Chaining operations with Result types allows for sequential error handling. This example demonstrates parsing a number and then dividing it, with each step returning a Result type.

const { ok, err, Result } = require('neverthrow');

function parseNumber(str) {
  const num = Number(str);
  return isNaN(num) ? err(new Error('Invalid number')) : ok(num);
}

function divide(a, b) {
  if (b === 0) {
    return err(new Error('Division by zero'));
  }
  return ok(a / b);
}

const result = parseNumber('4').andThen(num => divide(num, 2));
result.match({
  ok: value => console.log('Result:', value),
  err: error => console.error('Error:', error.message)
});

Other packages similar to neverthrow

Keywords

FAQs

Package last updated on 25 Oct 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc