New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More โ†’
Socket
Sign inDemoInstall
Socket

@sapphire/result

Package Overview
Dependencies
Maintainers
3
Versions
783
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@sapphire/result

Sapphire results package

  • 1.1.2-next.00da8f1.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
7.3K
increased by14.23%
Maintainers
3
Weekly downloads
ย 
Created
Source

Sapphire Logo

@sapphire/result

Result wrappers for function try-catch code blocks

GitHub codecov npm bundle size npm

Description

When having many try-catch blocks after one another, or multiple try-catch blocks nested in one another then code can become very chaotic very quickly. To alleviate that issue we have made the @sapphire/result which offers simple wrappers around such code. This code has been branched off of @sapphire/framework into its own package after we saw great success with the code there. Furthermore we also have wrappers for other arbitrary values in isMaybe, isSome, and isNone.

The code in this package has been greatly inspired by lexure from 1Computer1.

Features

  • Written in TypeScript
  • Bundled with esbuild so it can be used in NodeJS and browsers
  • Offers CommonJS, ESM and UMD bundles
  • Fully tested

Installation

You can use the following command to install this package, or replace npm install with your package manager of choice.

npm install @sapphire/result

Usage

Note: While this section uses require, the imports match 1:1 with ESM imports. For example const { from } = require('@sapphire/result') equals import { from } from '@sapphire/result'.

Wrapping synchronous try-catch blocks

Old code without from:

try {
	const returnCode = someFunctionThatMightThrow();

	return returnCode;
} catch (error) {
	// Handle the error, for example let it bubble up:
	throw new Error(error);
}

New Code with from:

const { from, isErr } = require('@sapphire/result');

const returnCode = from(() => someFunctionThatMightThrow());

if (isErr(returnCode)) {
	throw new Error(returnCode.error);
}

return returnCode.value;

Wrapping asynchronous try-catch blocks

Old code without fromAsync:

try {
	const returnCode = await someFunctionThatReturnsAPromiseAndMightReject();

	return returnCode;
} catch (error) {
	// Handle the error, for example let it bubble up:
	throw new Error(error);
}

New Code with fromAsync:

const { fromAsync, isErr } = require('@sapphire/result');

const returnCode = await fromAsync(async () => someFunctionThatReturnsAPromiseAndMightReject());

if (isErr(returnCode)) {
	throw new Error(returnCode.error);
}

return returnCode.value;

Buy us some doughnuts

Sapphire Community is and always will be open source, even if we don't get donations. That being said, we know there are amazing people who may still want to donate just to show their appreciation. Thank you very much in advance!

We accept donations through Open Collective, Ko-fi, Paypal, Patreon and GitHub Sponsorships. You can use the buttons below to donate through your method of choice.

Donate WithAddress
Open CollectiveClick Here
Ko-fiClick Here
PatreonClick Here
PayPalClick Here

Contributors โœจ

Thanks goes to these wonderful people (emoji key):


Jeroen Claassens

๐Ÿ’ป ๐Ÿš‡ ๐Ÿ“† ๐Ÿ“– โš ๏ธ

Antonio Romรกn

๐Ÿ’ป ๐Ÿ“† ๐Ÿ‘€ โš ๏ธ

Gryffon Bellish

๐Ÿ’ป ๐Ÿ‘€ โš ๏ธ

Vlad Frangu

๐Ÿ’ป ๐Ÿ› ๐Ÿ‘€ ๐Ÿ““ โš ๏ธ

Stitch07

๐Ÿ’ป ๐Ÿ“† โš ๏ธ

depfu[bot]

๐Ÿšง

allcontributors[bot]

๐Ÿ“–

Tyler J Russell

๐Ÿ“–

Ivan Lieder

๐Ÿ’ป ๐Ÿ›

Hezekiah Hendry

๐Ÿ’ป

Vetlix

๐Ÿ’ป

Ethan Mitchell

๐Ÿ“–

Elliot

๐Ÿ’ป

Jurien Hamaker

๐Ÿ’ป

Charalampos Fanoulis

๐Ÿ“–

dependabot[bot]

๐Ÿšง

Kaname

๐Ÿ’ป

nandhagk

๐Ÿ›

megatank58

๐Ÿ’ป

UndiedGamer

๐Ÿ’ป

Lioness100

๐Ÿ“– ๐Ÿ’ป

David

๐Ÿ’ป

renovate[bot]

๐Ÿšง

WhiteSource Renovate

๐Ÿšง

FC

๐Ÿ’ป

Jรฉrรฉmy de Saint Denis

๐Ÿ’ป

MrCube

๐Ÿ’ป

bitomic

๐Ÿ’ป

c43721

๐Ÿ’ป

Commandtechno

๐Ÿ’ป

Aura

๐Ÿ’ป

Jonathan

๐Ÿ’ป

Parbez

๐Ÿšง

Paul Andrew

๐Ÿ“–

This project follows the all-contributors specification. Contributions of any kind welcome!

Keywords

FAQs

Package last updated on 21 Apr 2022

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