@shiguww/result-js
v0.0.0
Published
A simple npm package that implements the `Option` and `Result` types from Rust. ```shell npm install @shiguww/result-js ```
Downloads
11
Readme
result-js
A simple npm package that implements the Option
and Result
types from Rust.
npm install @shiguww/result-js
Usage
// index.ts
const JSONParse = <T>(s: string) => JSON.parse(s) as T;
const readfile = Result.wrap((path: string) => fs.readFile(path, "utf8"));
const pkg = await readfile("package.json")
.then((res) => res.chain((s) => JSONParse<{ version: string }>(s)))
.then((res) => res.inspectErr(() => console.log("Couldn't parse package.json")))
.then((res) => res.catch(() => null))
.then((res) => res.unwrap());
if (pkg !== null) {
console.log(`package.json version: ${pkg.version}`);
}