@00f2ff/result

Safe promise handling with Results, implemented with ES2020 features

Latest version: 1.0.0 registry icon
Maintenance score
0
Safety score
0
Popularity score
0
Check your open source dependency risks. Get immediate insight about security, stability and licensing risks.
Security
  Vulnerabilities
Version Suggest Low Medium High Critical
1.0.0 0 0 0 0 0
0.1.0 0 0 0 0 0

Stability
Latest release:

1.0.0 - This version may not be safe as it has not been updated for a long time. Find out if your coding project uses this component and get notified of any reported security vulnerabilities with Meterian-X Open Source Security Platform

Licensing

Maintain your licence declarations and avoid unwanted licences to protect your IP the way you intended.

MIT   -   MIT License

Not a wildcard

Not proprietary

OSI Compliant



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