@rg-dev/stdlib 1.0.5 → 1.0.6

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.
@@ -20,6 +20,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
20
20
  var common_env_exports = {};
21
21
  __export(common_env_exports, {
22
22
  Optional: () => Optional,
23
+ catchInline: () => catchInline,
23
24
  doSafe: () => doSafe,
24
25
  isNumber: () => isNumber,
25
26
  isRunningOnServer: () => isRunningOnServer,
@@ -112,6 +113,13 @@ function promiseWithTimeout(promise, timeout, errMsg = "The promise timed out.")
112
113
  () => clearTimeout(timeoutID)
113
114
  );
114
115
  }
116
+ async function catchInline(cb) {
117
+ try {
118
+ return { error: void 0, result: await cb };
119
+ } catch (e) {
120
+ return { error: e, result: void 0 };
121
+ }
122
+ }
115
123
  function doSafe(safe, onError) {
116
124
  var _a;
117
125
  if (!safe)
@@ -21,8 +21,15 @@ declare class Optional<T> {
21
21
  declare function sleep(ms: number): Promise<unknown>;
22
22
  declare function isNumber(num: any): boolean;
23
23
  declare function promiseWithTimeout<T>(promise: Promise<T>, timeout: number, errMsg?: string): Promise<T>;
24
+ declare function catchInline<T>(cb: Promise<T>): Promise<{
25
+ error: undefined;
26
+ result: T;
27
+ } | {
28
+ error: Error;
29
+ result: undefined;
30
+ }>;
24
31
  declare function doSafe(safe: () => Promise<void> | void, onError?: (error: Error) => void): void;
25
32
  declare function isRunningOnServer(): boolean;
26
33
  declare function useServer(fn: () => (Promise<any> | void), onError?: (err: Error) => void): Promise<any>;
27
34
 
28
- export { type AsyncReturnType, type MyFn, Optional, doSafe, isNumber, isRunningOnServer, promiseRetry, promiseWithTimeout, sleep, useServer };
35
+ export { type AsyncReturnType, type MyFn, Optional, catchInline, doSafe, isNumber, isRunningOnServer, promiseRetry, promiseWithTimeout, sleep, useServer };
@@ -21,8 +21,15 @@ declare class Optional<T> {
21
21
  declare function sleep(ms: number): Promise<unknown>;
22
22
  declare function isNumber(num: any): boolean;
23
23
  declare function promiseWithTimeout<T>(promise: Promise<T>, timeout: number, errMsg?: string): Promise<T>;
24
+ declare function catchInline<T>(cb: Promise<T>): Promise<{
25
+ error: undefined;
26
+ result: T;
27
+ } | {
28
+ error: Error;
29
+ result: undefined;
30
+ }>;
24
31
  declare function doSafe(safe: () => Promise<void> | void, onError?: (error: Error) => void): void;
25
32
  declare function isRunningOnServer(): boolean;
26
33
  declare function useServer(fn: () => (Promise<any> | void), onError?: (err: Error) => void): Promise<any>;
27
34
 
28
- export { type AsyncReturnType, type MyFn, Optional, doSafe, isNumber, isRunningOnServer, promiseRetry, promiseWithTimeout, sleep, useServer };
35
+ export { type AsyncReturnType, type MyFn, Optional, catchInline, doSafe, isNumber, isRunningOnServer, promiseRetry, promiseWithTimeout, sleep, useServer };
package/lib/common-env.js CHANGED
@@ -80,6 +80,13 @@ function promiseWithTimeout(promise, timeout, errMsg = "The promise timed out.")
80
80
  () => clearTimeout(timeoutID)
81
81
  );
82
82
  }
83
+ async function catchInline(cb) {
84
+ try {
85
+ return { error: void 0, result: await cb };
86
+ } catch (e) {
87
+ return { error: e, result: void 0 };
88
+ }
89
+ }
83
90
  function doSafe(safe, onError) {
84
91
  var _a;
85
92
  if (!safe)
@@ -127,6 +134,7 @@ function isPromise(value) {
127
134
  }
128
135
  export {
129
136
  Optional,
137
+ catchInline,
130
138
  doSafe,
131
139
  isNumber,
132
140
  isRunningOnServer,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rg-dev/stdlib",
3
- "version": "1.0.5",
3
+ "version": "1.0.6",
4
4
  "description": "",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1",