go-go-try 1.0.0 → 2.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # `go-go-try`
2
2
 
3
- > Tries to execute a sync/async function, returns a specified default value if the function throws.
3
+ > Tries to execute a sync/async function, returns a Golang style result.
4
4
 
5
5
  [![Gzipped Size](https://img.shields.io/bundlephobia/minzip/good-try)](https://bundlephobia.com/result?p=good-try)
6
6
  [![Build Status](https://img.shields.io/github/workflow/status/astoilkov/good-try/CI)](https://github.com/astoilkov/good-try/actions/workflows/main.yml)
@@ -10,7 +10,6 @@
10
10
  Why not [`nice-try`](https://github.com/electerious/nice-try) with it's 70+ million downloads per month?
11
11
 
12
12
  - `go-go-try` supports async functions.
13
- - `go-go-try` supports an optional default value.
14
13
  - `go-go-try` allows you to capture the thrown error.
15
14
  - `go-go-try` is written in TypeScript. The types are written in a way that reduce developer errors.
16
15
  - `go-go-try` is inspired by Golang error catching.
@@ -41,13 +40,13 @@ npm install go-go-try
41
40
  import goTry from 'go-go-try'
42
41
 
43
42
  // tries to parse todos, returns empty array if it fails
44
- const [_, value] = goTry(() => JSON.parse(todos), [])
43
+ const [_, value = []] = goTry(() => JSON.parse(todos))
45
44
 
46
45
  // fetch todos, on error, fallback to empty array
47
- const [_, todos] = await goTry(fetchTodos(), [])
46
+ const [_, todos = []] = await goTry(fetchTodos())
48
47
 
49
48
  // fetch todos, fallback to empty array, send error to your error tracking service
50
- const [err, todos] = await goTry(fetchTodos(), [])
49
+ const [err, todos = []] = await goTry(fetchTodos())
51
50
  sentToErrorTrackingService(err)
52
51
  ```
53
52
 
@@ -58,11 +57,8 @@ sentToErrorTrackingService(err)
58
57
  - synchronous function `goTry(() => JSON.parse(value))`
59
58
  - asynchronous function / Promise
60
59
 
61
- **Second parameter** accepts:
62
-
63
- - a value of the same type of the first parameter that will be returned if the first parameter throws
64
-
65
60
  **Returns** a tuple with the possible error and result (Golang style)
61
+
66
62
  If you use TypeScript, the types are well defined and won't let you make a mistake.
67
63
 
68
64
  ## Inspiration
package/index.d.ts CHANGED
@@ -1,4 +1,3 @@
1
- declare type ResultTuple<T> = [string?, T?];
2
- export default function goTry<T>(value: Promise<T>, defaultValue?: T): Promise<ResultTuple<T>>;
3
- export default function goTry<T>(value: () => T, defaultValue?: T): ResultTuple<T>;
1
+ declare type ResultTuple<T> = [string, undefined] | [undefined, T];
2
+ export default function goTry<T>(value: (() => T) | PromiseLike<T>): ResultTuple<T> | PromiseLike<ResultTuple<T>>;
4
3
  export {};
package/index.js CHANGED
@@ -21,17 +21,18 @@ function toErrorWithMessage(maybeError) {
21
21
  function getErrorMessage(error) {
22
22
  return toErrorWithMessage(error).message;
23
23
  }
24
- export default function goTry(value, defaultValue) {
24
+ export default function goTry(value) {
25
+ let unwrappedValue;
25
26
  try {
26
- const unwrappedValue = typeof value === 'function' ? value() : value;
27
+ unwrappedValue = typeof value === 'function' ? value() : value;
27
28
  if (pIsPromise(unwrappedValue)) {
28
29
  return Promise.resolve(unwrappedValue)
29
30
  .then((value) => [undefined, value])
30
- .catch((err) => [getErrorMessage(err), defaultValue]);
31
+ .catch((err) => [getErrorMessage(err), undefined]);
31
32
  }
32
33
  return [undefined, unwrappedValue];
33
34
  }
34
35
  catch (err) {
35
- return [getErrorMessage(err), defaultValue];
36
+ return [getErrorMessage(err), undefined];
36
37
  }
37
38
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "go-go-try",
3
- "version": "1.0.0",
3
+ "version": "2.0.2",
4
4
  "description": "Tries to execute a sync/async function, returns a result tuple",
5
5
  "license": "MIT",
6
6
  "repository": "thelinuxlich/go-go-try",