Skip to content

microlib-js/try

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

33 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿ“ฆ @microlib/try

npm version npm downloads Github Actions Bundlephobia

A Promise like quasi monad but for Try.

๐Ÿš€ Installation

npm install @microlib/try
bun add @microlib/try

๐Ÿ“– Usage

import { Try } from "@microlib/try";

function task() {
  if (Math.random() > 0.5) {
    throw new Error("Something went wrong");
  } else {
    return "Hello";
  }
}

const result = Try(task)
  .catch(() => "Bye")
  .then((v) => v + ", World!");

if (result.ok) {
  console.log(result.value);
}

/* 

Output can be any of these:
- Hello, World!
- Bye, World!

*/

๐Ÿ€ Show your Support

Give a โญ๏ธ if this project helped you!