@naturalcycles/js-lib 14.78.0 → 14.78.1
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.
|
@@ -17,6 +17,14 @@ export interface PTimeoutOptions {
|
|
|
17
17
|
* Can be used to thrown a custom error OR resolve a promise without throwing.
|
|
18
18
|
*/
|
|
19
19
|
onTimeout?: () => any;
|
|
20
|
+
/**
|
|
21
|
+
* Defaults to true.
|
|
22
|
+
* If true - preserves the stack trace in case of a Timeout (usually - very useful!).
|
|
23
|
+
* It has a certain perf cost.
|
|
24
|
+
*
|
|
25
|
+
* @experimental
|
|
26
|
+
*/
|
|
27
|
+
keepStackTrace?: boolean;
|
|
20
28
|
}
|
|
21
29
|
/**
|
|
22
30
|
* Decorates a Function with a timeout.
|
package/dist/promise/pTimeout.js
CHANGED
|
@@ -24,7 +24,8 @@ exports.pTimeoutFn = pTimeoutFn;
|
|
|
24
24
|
*/
|
|
25
25
|
async function pTimeout(promise, opt) {
|
|
26
26
|
// todo: check how we can automatically infer function name (only applicable to named functions)
|
|
27
|
-
const { timeout, name, onTimeout } = opt;
|
|
27
|
+
const { timeout, name, onTimeout, keepStackTrace = true } = opt;
|
|
28
|
+
const fakeError = keepStackTrace ? new Error('TimeoutError') : undefined;
|
|
28
29
|
// eslint-disable-next-line no-async-promise-executor
|
|
29
30
|
return await new Promise(async (resolve, reject) => {
|
|
30
31
|
// Prepare the timeout timer
|
|
@@ -34,11 +35,16 @@ async function pTimeout(promise, opt) {
|
|
|
34
35
|
resolve(onTimeout());
|
|
35
36
|
}
|
|
36
37
|
catch (err) {
|
|
38
|
+
if (fakeError)
|
|
39
|
+
err.stack = fakeError.stack; // keep original stack
|
|
37
40
|
reject(err);
|
|
38
41
|
}
|
|
39
42
|
return;
|
|
40
43
|
}
|
|
41
|
-
|
|
44
|
+
const err = new TimeoutError(`"${name || 'pTimeout function'}" timed out after ${timeout} ms`);
|
|
45
|
+
if (fakeError)
|
|
46
|
+
err.stack = fakeError.stack; // keep original stack
|
|
47
|
+
reject(err);
|
|
42
48
|
}, timeout);
|
|
43
49
|
// Execute the Function
|
|
44
50
|
try {
|
|
@@ -19,7 +19,8 @@ export function pTimeoutFn(fn, opt) {
|
|
|
19
19
|
*/
|
|
20
20
|
export async function pTimeout(promise, opt) {
|
|
21
21
|
// todo: check how we can automatically infer function name (only applicable to named functions)
|
|
22
|
-
const { timeout, name, onTimeout } = opt;
|
|
22
|
+
const { timeout, name, onTimeout, keepStackTrace = true } = opt;
|
|
23
|
+
const fakeError = keepStackTrace ? new Error('TimeoutError') : undefined;
|
|
23
24
|
// eslint-disable-next-line no-async-promise-executor
|
|
24
25
|
return await new Promise(async (resolve, reject) => {
|
|
25
26
|
// Prepare the timeout timer
|
|
@@ -29,11 +30,16 @@ export async function pTimeout(promise, opt) {
|
|
|
29
30
|
resolve(onTimeout());
|
|
30
31
|
}
|
|
31
32
|
catch (err) {
|
|
33
|
+
if (fakeError)
|
|
34
|
+
err.stack = fakeError.stack; // keep original stack
|
|
32
35
|
reject(err);
|
|
33
36
|
}
|
|
34
37
|
return;
|
|
35
38
|
}
|
|
36
|
-
|
|
39
|
+
const err = new TimeoutError(`"${name || 'pTimeout function'}" timed out after ${timeout} ms`);
|
|
40
|
+
if (fakeError)
|
|
41
|
+
err.stack = fakeError.stack; // keep original stack
|
|
42
|
+
reject(err);
|
|
37
43
|
}, timeout);
|
|
38
44
|
// Execute the Function
|
|
39
45
|
try {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@naturalcycles/js-lib",
|
|
3
|
-
"version": "14.78.
|
|
3
|
+
"version": "14.78.1",
|
|
4
4
|
"scripts": {
|
|
5
5
|
"prepare": "husky install",
|
|
6
6
|
"build-prod": "build-prod-esm-cjs",
|
|
@@ -14,13 +14,14 @@
|
|
|
14
14
|
"@naturalcycles/bench-lib": "^1.5.0",
|
|
15
15
|
"@naturalcycles/dev-lib": "^12.0.0",
|
|
16
16
|
"@naturalcycles/nodejs-lib": "^12.33.4",
|
|
17
|
-
"@types/node": "^
|
|
17
|
+
"@types/node": "^17.0.4",
|
|
18
18
|
"jest": "^27.0.1",
|
|
19
19
|
"patch-package": "^6.2.1",
|
|
20
20
|
"prettier": "^2.1.2",
|
|
21
21
|
"rxjs": "^7.0.1",
|
|
22
22
|
"vuepress": "^1.7.1",
|
|
23
|
-
"vuepress-plugin-typescript": "^0.3.1"
|
|
23
|
+
"vuepress-plugin-typescript": "^0.3.1",
|
|
24
|
+
"weak-napi": "^2.0.2"
|
|
24
25
|
},
|
|
25
26
|
"files": [
|
|
26
27
|
"dist",
|
package/src/promise/pTimeout.ts
CHANGED
|
@@ -20,6 +20,15 @@ export interface PTimeoutOptions {
|
|
|
20
20
|
* Can be used to thrown a custom error OR resolve a promise without throwing.
|
|
21
21
|
*/
|
|
22
22
|
onTimeout?: () => any
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Defaults to true.
|
|
26
|
+
* If true - preserves the stack trace in case of a Timeout (usually - very useful!).
|
|
27
|
+
* It has a certain perf cost.
|
|
28
|
+
*
|
|
29
|
+
* @experimental
|
|
30
|
+
*/
|
|
31
|
+
keepStackTrace?: boolean
|
|
23
32
|
}
|
|
24
33
|
|
|
25
34
|
/**
|
|
@@ -42,7 +51,8 @@ export function pTimeoutFn<T extends AnyFunction>(fn: T, opt: PTimeoutOptions):
|
|
|
42
51
|
*/
|
|
43
52
|
export async function pTimeout<T>(promise: Promise<T>, opt: PTimeoutOptions): Promise<T> {
|
|
44
53
|
// todo: check how we can automatically infer function name (only applicable to named functions)
|
|
45
|
-
const { timeout, name, onTimeout } = opt
|
|
54
|
+
const { timeout, name, onTimeout, keepStackTrace = true } = opt
|
|
55
|
+
const fakeError = keepStackTrace ? new Error('TimeoutError') : undefined
|
|
46
56
|
|
|
47
57
|
// eslint-disable-next-line no-async-promise-executor
|
|
48
58
|
return await new Promise(async (resolve, reject) => {
|
|
@@ -51,13 +61,16 @@ export async function pTimeout<T>(promise: Promise<T>, opt: PTimeoutOptions): Pr
|
|
|
51
61
|
if (onTimeout) {
|
|
52
62
|
try {
|
|
53
63
|
resolve(onTimeout())
|
|
54
|
-
} catch (err) {
|
|
64
|
+
} catch (err: any) {
|
|
65
|
+
if (fakeError) err.stack = fakeError.stack // keep original stack
|
|
55
66
|
reject(err)
|
|
56
67
|
}
|
|
57
68
|
return
|
|
58
69
|
}
|
|
59
70
|
|
|
60
|
-
|
|
71
|
+
const err = new TimeoutError(`"${name || 'pTimeout function'}" timed out after ${timeout} ms`)
|
|
72
|
+
if (fakeError) err.stack = fakeError.stack // keep original stack
|
|
73
|
+
reject(err)
|
|
61
74
|
}, timeout)
|
|
62
75
|
|
|
63
76
|
// Execute the Function
|