https://github.com/constraintautomaton/result-interface-ts
A tiny utility to standardize how to handle results that may succeed or fail inspired by Go-style error handling.
Science Score: 26.0%
This score indicates how likely this project is to be science-related based on various indicators:
-
○CITATION.cff file
-
✓codemeta.json file
Found codemeta.json file -
✓.zenodo.json file
Found .zenodo.json file -
○DOI references
-
○Academic publication links
-
○Academic email domains
-
○Institutional organization owner
-
○JOSS paper metadata
-
○Scientific vocabulary similarity
Low similarity (8.3%) to scientific vocabulary
Keywords
Repository
A tiny utility to standardize how to handle results that may succeed or fail inspired by Go-style error handling.
Basic Info
- Host: GitHub
- Owner: constraintAutomaton
- License: mit
- Language: TypeScript
- Default Branch: main
- Homepage: https://www.npmjs.com/package/result-interface
- Size: 48.8 KB
Statistics
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
- Releases: 0
Topics
Metadata Files
README.md
result-interface
A tiny utility (mainly interfaces) with zero dependencies to standardize handling results that may succeed or fail, inspired by Go-style error handling.
Installation
bash
npm i result-interface
Usage
You can define functions more declaratively when working with possible failures:
```ts import { type Result, isError } from "result-interface";
let VALUE: number | undefined = undefined;
function getValue(): Result
const resp: Result
if (isError(resp)) {
console.log(Unable to get the value. Reason: ${resp.error});
process.exit(1);
}
console.log(The value multiplied by two is ${resp.value * 2});
```
You can use helper functions to generate IError and IResult types (the possible types of Result).
```ts import { type Result, isError, result, error } from "result-interface";
function getValue(): Result
You can specify that a Promise will not throw:
```ts import { isError, SafePromise, type Result } from "result-interface";
let VALUE: number | undefined = undefined;
async function getValueLater(): SafePromise
// Creates a promise that always resolves with a Result.
// On failure, it resolves with an error instead of rejecting or throwing.
const resp: Result
if (isError(resp)) {
console.log(Unable to get the value. Reason: ${resp.error});
process.exit(1);
}
console.log(The value multiplied by two is ${resp.value * 2});
```
You can specify that a promise will never fail, thus that it will always be an IResult:
```ts
import { SafePromise, type Result } from "./src/index";
let VALUE: number | undefined = undefined;
async function getValueLater(): SafePromise
// Creates a promise that always resolves with a Result.
// On failure, it resolves with an error instead of rejecting or throwing.
const resp: Result
console.log(The value multiplied by two is ${resp.value * 2});
``
You can ensure that aPromise` will not throw:
```ts import { isError, safePromise, type Result } from "result-interface";
let VALUE: number | undefined = undefined;
async function getValueLaterUnsafe(): Promise
// Creates a promise that always resolves with a Result.
// On failure, it resolves with an error instead of rejecting or throwing.
const resp: Result
if (isError(resp)) {
console.log(Unable to get the value. Reason: ${resp.error});
process.exit(1);
}
console.log(The value multiplied by two is ${resp.value * 2});
```
Test
bash
bun test
License
This project is licensed under the MIT License. See the LICENSE file for more details.
Owner
- Name: Bryan-Elliott Tam
- Login: constraintAutomaton
- Kind: user
- Location: Ghent, Belgium
- Company: imec - Ghent University - IDLab
- Repositories: 8
- Profile: https://github.com/constraintAutomaton
PhD Student working on querying for semantic web technologies
GitHub Events
Total
- Push event: 35
- Create event: 2
Last Year
- Push event: 35
- Create event: 2
Issues and Pull Requests
Last synced: 7 months ago
All Time
- Total issues: 0
- Total pull requests: 0
- Average time to close issues: N/A
- Average time to close pull requests: N/A
- Total issue authors: 0
- Total pull request authors: 0
- Average comments per issue: 0
- Average comments per pull request: 0
- Merged pull requests: 0
- Bot issues: 0
- Bot pull requests: 0
Past Year
- Issues: 0
- Pull requests: 0
- Average time to close issues: N/A
- Average time to close pull requests: N/A
- Issue authors: 0
- Pull request authors: 0
- Average comments per issue: 0
- Average comments per pull request: 0
- Merged pull requests: 0
- Bot issues: 0
- Bot pull requests: 0
Top Authors
Issue Authors
Pull Request Authors
Top Labels
Issue Labels
Pull Request Labels
Packages
- Total packages: 1
-
Total downloads:
- npm 629 last-month
- Total dependent packages: 0
- Total dependent repositories: 0
- Total versions: 20
- Total maintainers: 1
npmjs.org: result-interface
A tiny utility (mainly interfaces) with zero dependencies to standardize handling results that may succeed or fail, inspired by Go-style error handling.
- Homepage: https://github.com/constraintAutomaton/result-interface-ts
- License: MIT
-
Latest release: 2.3.3
published 7 months ago