Skip to content

Latest commit

 

History

History
63 lines (46 loc) · 1.5 KB

README.md

File metadata and controls

63 lines (46 loc) · 1.5 KB

📦 @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!