Catalog npm
PACKAGE EXPLORERView in registry

@00f2ff/result

npm

Safe promise handling with Results, implemented with ES2020 features

result
Versions collected2
Direct dependencies14From release 1.0.0
Last observed20 Oct, 21:54 UTC

README

Current package documentation

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

Downloads & popularity

Awaiting observations

No metric observations yet

Download counts and popularity will appear here as the collector receives them.

Versions 2

Collected releases
VersionPublishedDirect dependenciesOpen version
1.0.0Registry latest27 Feb 202314
0.1.027 Feb 202311

Package advisories

0
No matching advisories collected

This describes our collected data; it is not a security assessment.

Repository

PENDING
00f2ff/result

Shared repository data · GitHub

License detected in repository
Not collected

Metadata history

Meaningful changes
Current collected state Current

Observed since 20 Oct, 21:54 UTC

No superseded metadata states collected. Unchanged observations do not create new revisions.

Record identity

package:6a47adf7b096b763480a2ff10486b9dc05f66bd985c54f0c017000e74fbb6ed9