https://github.com/bbb/result4t
A replacement for Promise that gives us a strongly-typed failure mode.
https://github.com/bbb/result4t
fp result result-type typescript
Last synced: 3 months ago
JSON representation
A replacement for Promise that gives us a strongly-typed failure mode.
- Host: GitHub
- URL: https://github.com/bbb/result4t
- Owner: BBB
- Created: 2022-07-15T17:09:59.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2023-12-07T16:09:10.000Z (about 2 years ago)
- Last Synced: 2025-09-30T00:44:27.057Z (4 months ago)
- Topics: fp, result, result-type, typescript
- Language: TypeScript
- Homepage:
- Size: 884 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
result4t
Available now
`pnpm add @ollierelph/result4t` or `pnpm add result4t`
## What's this?
`result4t` takes some pointers from the wonderful [result4k](https://github.com/fork-handles/forkhandles/tree/trunk/result4k)
It's a replacement for `Promise` that gives us a strongly-typed error mode.
## An example
```typescript
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`:
```typescript
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();
```