https://github.com/morglod/go-result-js
Go like results in js
https://github.com/morglod/go-result-js
result-format result-type
Last synced: 5 months ago
JSON representation
Go like results in js
- Host: GitHub
- URL: https://github.com/morglod/go-result-js
- Owner: Morglod
- Created: 2018-06-26T13:52:30.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2019-06-25T01:57:38.000Z (about 6 years ago)
- Last Synced: 2025-02-09T13:45:26.533Z (5 months ago)
- Topics: result-format, result-type
- Language: TypeScript
- Size: 22.5 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
[](https://www.npmjs.com/package/go-result-js)
[](https://codecov.io/gh/Morglod/go-result-js)# go-result-js
Go-like results:
```js
const [ err, result ] = foo();
```* Zero dependencies
* Full typescript support
* 100% test coverage## Usage
Install:
```sh
npm i go-result-js
```Import:
```ts
import { ResultA, ResultErr, ResultOk } from 'go-result-js';
```Or use it globally:
(write in main file, before everythink)
```ts
import 'go-result-js/lib/global';
```## Example with exceptions
Before:
```ts
const result = await doSmthWithException();
// (node:17320) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): Error: Request failed with status code 400
// (node:17320) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
```After:
```ts
const [ err, result ] = await ResultA(doSmthWithException());
if (err || !result) {
// exception handled!
}
```## Example
```ts
function divide(a: number, b: number): Result {
if (b === 0) return ResultErr('Can not divide by zero!');
return ResultOk(a / b);
}function main() {
const [ err, result ] = divide(10, 5);if (err) console.error(err);
else console.log('success', result);
}
```## Example with async
```ts
const asyncDivide = (a: number, b: number): ResultA => ResultA(resolve => {
resolve(b === 0 ? new Error('Can not divide by zero!') : a / b);
});async function asyncMain() {
const [ err, result ] = await asyncDivide(10, 5);if (err) console.error(err);
else console.log('success', result);
}
```## Example with AWS
```ts
import { Auth } from 'aws-amplify';export async function main() {
const [ err, user ] = await ResultA(Auth.signIn('root', 'password'));
}
```## Multiple results
Handle multiple `ResultA` results, and catch any errors.
```ts
const { err, values } = await resultAll(
Content.fetchById(contentType, id),
Rubrics.fetchRubrics(),
ContentTypes.fetchContentTypes(),
);if (err) {
// on any error
}const {
0: content,
1: rubrics,
2: contentTypes,
} = values;
```## API
Types
```ts
export type ResultErr = ErrorT|true|undefined;
export type Result = [ ResultErr, T|undefined ];
export type ResultA = Promise>;
```Methods
```ts
function registerGlobally(global?: any): Result
```Assign all methods to `global` object.
`go-result-js/lib/global` calls it.```ts
function ResultOk(value: T): Result
```Returns `value` with undefined error.
```ts
function ResultErr(err: ErrorT|true|string = true): Result
```Returns `error` with undefined value.
```ts
function ResultA(
x: Promise | ((
resolve: (value: undefined|T|Error) => void,
reject: (err?: ErrorT) => void
) => void)
): ResultA
```Takes `Promise's callbacks` or `Promise`, catch it's error and resolve as `Promise`.
```ts
function resultAll[]>(
...results: Results
): Promise<{
err: ResultErr | undefined,
values: {
[i in keyof Results]: PickSecondItem, undefined>
},
results: {
[i in keyof Results]: PromiseType
}
}>
```