result4t
v0.7.0
Published
<h2 align="center">result4t</h2>
Downloads
8
Maintainers
Readme
pnpm add @ollierelph/result4t
or pnpm add result4t
What's this?
result4t
takes some pointers from the wonderful result4k
It's a replacement for Promise
that gives us a strongly-typed error mode.
An example
import fs from "node:fs";
const readFileAndReverse = () =>
fs
.readFile("./boom.txt")
.then((contents) => contents.split("\n").reverse().join("\n"));
readFileAndReverse()
.then((reversedFile) => {
console.log(reversedFile);
process.exit(0);
})
.catch((error: unknown) => {
console.error(error);
process.exit(1);
});
in result4t
:
import fs from "node:fs";
import { TaskResult } from "./TaskResult";
const readFileAndReverse = () =>
TaskResult.ofPromise(
() => fs.readFile("./boom.txt"),
(err: unknown) => new Error("Unable to find file"),
).map((contents) => contents.split("\n").reverse().join("\n"));
readFileAndReverse()
.peek((reversedFile) => {
console.log(reversedFile);
process.exit(0);
})
.peekLeft((error) => {
// ^--- typed as Error
console.error(error);
process.exit(1);
})
.run();