Socket
Socket
Sign inDemoInstall

@00f2ff/result

Package Overview
Dependencies
0
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @00f2ff/result

Safe promise handling with Results, implemented with ES2020 features


Version published
Weekly downloads
12
increased by200%
Maintainers
1
Created
Weekly downloads
 

Readme

Source

Result

Safely handle asynchronous errors with Result. Inspired by neverthrow's core ResultAsync types, but is intended for use in sequential imperative statements using async/await, rather than functional pipelining.

Akin to Rust's std::result and Scala's scala.util.Either. Uses ES2020's Promise.allSettled under the hood and mostly follows its naming convention.

Installation

npm i @00f2ff/result

API

E defaults to Error.

settle<T, E>

Settles a promise into Result<T, E> that can be narrowed to Fulfilled<T, E> or Rejected<T, E>.

const result: Result<boolean, Error> = await settle(Promise.resolve(true));
if (result.isFulfilled()) { 
  console.log(result.value); // true
}
const result: Result<boolean, Error> = await settle(Promise.reject(new Error("oh no"));
if (result.isRejected()) { 
  console.log(result.error); // Error("oh no")
}

settleAll<T, E>

Settles an array of promises into a tuple of [Fulfilled<T, E>[], Rejected<T, E>[]].

const resolvingPromises = [Promise.resolve(true), Promise.resolve("hello")];
const rejectingPromises = [Promise.reject(new Error("oh no"), Promise.reject(new DbError("ruh roh"))];

const [fulfilled, rejected] = await settleAll<boolean | string, Error | DbError>([...resolvingPromises, ...rejectingPromises]);

Test

npm run test

Keywords

FAQs

Last updated on 27 Feb 2023

Did you know?

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc