npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

rescript-result-exn

v0.1.1

Published

provides api for result<'a, exn> type

Downloads

4

Readme

rescript-result-exn

Provides api for result<'a, exn> type. Extends Belt.Result api, and adds more functions for converting reult with option monads and catching exceptions from throwable functions and wrapping them to Error(exn).

Api

ResultExn.resi

type resultexn<'a> = result<'a, exn>

//when res is Ok('n) returns 'n, when res is Error('exn), raise an 'exn
@inline
let getExn: resultexn<'a> => 'a

exception ResultIsNotAnError
//when res is Ok(_) raises ResultIsNotAnError, when res is Error(exn) return exn
@inline
let getErr: resultexn<'a> => exn

//when res is Ok('n), returns f('n), otherwise default.
@inline
let mapWithDefault: (resultexn<'a>, 'b, 'a => 'b) => 'b

//mapWithDefault, but with uncurried function
@inline
let mapWithDefaultU: (resultexn<'a>, 'b, (. 'a) => 'b) => 'b

//When res is Ok(n), returns Ok(f(n)). Otherwise returns res unchanged.
//Function f takes a value of the same type as n and returns an ordinary value.
@inline
let map: (resultexn<'a>, 'a => 'b) => resultexn<'b>

//When r1 is Ok('a) and r2 is Ok('b) returns Ok(f('a, 'b)). Otherwise returns Error(exn).
@inline
let map2: (resultexn<'a>, resultexn<'b>, ('a, 'b) => 'c) => resultexn<'c>

//When r1 is Ok('a) and r2 is Ok('b) and r3 is (Ok('c)) returns Ok(f('a, 'b, 'c)).
//Otherwise returns Error(exn) (exception of first error parameter meets).
@inline
let map3: (resultexn<'a>, resultexn<'b>, resultexn<'c>, ('a, 'b, 'c) => 'd) => resultexn<'d>

//When all parameters 'a, 'b, 'c, 'd is Ok returns Ok(f('a, 'b, 'c, 'd)).
//Otherwise returns Error(exn) (exception of first error parameter meets).
@inline
let map4: (
  resultexn<'a>,
  resultexn<'b>,
  resultexn<'c>,
  resultexn<'d>,
  ('a, 'b, 'c, 'd) => 'e,
) => resultexn<'e>

//map, but with uncurried function
@inline
let mapU: (resultexn<'a>, (. 'a) => 'b) => resultexn<'b>

//Async version of mapU function
@inline
let mapUAsync: (resultexn<'a>, (. 'a) => promise<'b>) => promise<resultexn<'b>>

//Async version of map function
@inline
let mapAsync: (resultexn<'a>, 'a => promise<'b>) => promise<resultexn<'b>>

//Async version of map2 function
@inline
let map2Async: (resultexn<'a>, resultexn<'b>, ('a, 'b) => promise<'c>) => promise<resultexn<'c>>

//Async version of map3 function
@inline
let map3Async: (
  resultexn<'a>,
  resultexn<'b>,
  resultexn<'c>,
  ('a, 'b, 'c) => promise<'d>,
) => promise<resultexn<'d>>

//Async version of map4 function
@inline
let map4Async: (
  resultexn<'a>,
  resultexn<'b>,
  resultexn<'c>,
  resultexn<'d>,
  ('a, 'b, 'c, 'd) => promise<'e>,
) => promise<resultexn<'e>>

//When res is Ok('n), returns f('n). Otherwise, returns res unchanged.
//Function f takes a value of the same type as 'n and returns a resultexn<'b>
@inline
let flatMap: (resultexn<'a>, 'a => resultexn<'b>) => resultexn<'b>

//When r1 is Ok('a), r2 is Ok('b) return f('a, 'b). Otherwise return Error(exn) (- first exn param)
@inline
let flatMap2: (resultexn<'a>, resultexn<'b>, ('a, 'b) => resultexn<'c>) => resultexn<'c>

//When r1 is Ok('a), r2 is Ok('b) and r3 is Ok('c) return f('a, 'b, 'c).
//Otherwise return Error(exn) (- first exn param)
@inline
let flatMap3: (
  resultexn<'a>,
  resultexn<'b>,
  resultexn<'c>,
  ('a, 'b, 'c) => resultexn<'d>,
) => resultexn<'d>

//When r1 is Ok('a), r2 is Ok('b), r3 is Ok('c) and r4 is Ok('d) return f('a, 'b, 'c, 'd).
//Otherwise return Error(exn) (- first exn param)
@inline
let flatMap4: (
  resultexn<'a>,
  resultexn<'b>,
  resultexn<'c>,
  resultexn<'d>,
  ('a, 'b, 'c, 'd) => resultexn<'e>,
) => resultexn<'e>

//flatMap, but with uncurried function
@inline
let flatMapU: (resultexn<'a>, (. 'a) => resultexn<'b>) => resultexn<'b>

//Async version of flatMapU function
@inline
let flatMapUAsync: (resultexn<'a>, (. 'a) => promise<resultexn<'b>>) => promise<resultexn<'b>>

//Async version of flatMap function
@inline
let flatMapAsync: (resultexn<'a>, 'a => promise<resultexn<'b>>) => promise<resultexn<'b>>

//Async version of flatMap2 function
@inline
let flatMap2Async: (
  resultexn<'a>,
  resultexn<'b>,
  ('a, 'b) => promise<resultexn<'c>>,
) => promise<resultexn<'c>>

//Async version of flatMap3 function
@inline
let flatMap3Async: (
  resultexn<'a>,
  resultexn<'b>,
  resultexn<'c>,
  ('a, 'b, 'c) => promise<resultexn<'d>>,
) => promise<resultexn<'d>>

//Async version of flatMap4 function
@inline
let flatMap4Async: (
  resultexn<'a>,
  resultexn<'b>,
  resultexn<'c>,
  resultexn<'d>,
  ('a, 'b, 'c, 'd) => promise<resultexn<'e>>,
) => promise<resultexn<'e>>

//If res is Ok('n), returns 'n, otherwise default
@inline
let getWithDefault: (resultexn<'a>, 'a) => 'a

//Returns true if res is of the form Ok('n), false if it is the Error('e) variant.
@inline
let isOk: resultexn<'a> => bool

//Returns true if res is of the form Error(e), false if it is the Ok(n) variant.
@inline
let isError: resultexn<'a> => bool

//converts array<resultexn<'a>> to resultexn<array<'a>>
let lift: array<resultexn<'a>> => resultexn<array<'a>>

exception TryToGettingResultFromNone
//Converts option<'a> to result<'a, exn>
@inline
let fromSome: option<'a> => resultexn<'a>

exception UnexpectedSomeValue(string)
//Converts option<'a> to result<unit, exn>
@inline
let fromNone: option<'a> => resultexn<unit>

//Converts result<'a, exn> to option<'a>
@inline
let okToOption: resultexn<'a> => option<'a>

//Converts result<'a, exn> to option<exn>
@inline
let errToOption: resultexn<'a> => option<exn>

//Exectes the function f: 'a => 'b and catching and wrapping raised errors to Error(exn)
@inline
let tryExec: (unit => 'a) => resultexn<'a>

//Exectes the function f: 'a => result<'b, exn> and catching and wrapping raised errors to Error(exn)
@inline
let tryExecFlat: (unit => resultexn<'a>) => resultexn<'a>

//Promise will return result<'a, exn> where exn is catched exception
@inline
let thenTry: promise<'a> => promise<resultexn<'a>>

//Catches extra exceptions, which will happens at promise<result<'a, exn>> execution
@inline
let thenTryFlat: promise<resultexn<'a>> => promise<resultexn<'a>>

//Converts promise<result<'a, exn>> with throwable function and catch exception into Error
@inline
let thenTryMap: (promise<resultexn<'a>>, 'a => 'b) => promise<resultexn<'b>>

//Converts promise<result<'a, exn>> with throwable resulting function and catch exception into Error
@inline
let thenTryFlatMap: (promise<resultexn<'a>>, 'a => resultexn<'b>) => promise<resultexn<'b>>

//Chains promise<result<'a, exn>> with 'a => promise<'b> map funciton
@inline
let thenTryMapAsync: (promise<resultexn<'a>>, 'a => promise<'b>) => promise<resultexn<'b>>

//Chains promise<result<'a, exn>> with 'a => promise<result<'b, exn>> map funciton
@inline
let thenTryFlatMapAsync: (
  promise<resultexn<'a>>,
  'a => promise<resultexn<'b>>,
) => promise<resultexn<'b>>

License

  • MIT
  • Uses lot of Belt package of rescript, thats is licensed under LGPL version 3

Author

Anatoly Starodubtsev [email protected]

Uses lot of Belt package of rescript, thats had been created by Hongbo Zhang.

Original Belt's api and list of authors and contributors may been found of site: https://rescript-lang.org/docs/manual/latest/api/belt