@naturalcycles/js-lib 14.116.0 → 14.117.0

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.
@@ -16,8 +16,11 @@ export interface PTimeoutOptions {
16
16
  /**
17
17
  * If provided - will be called INSTEAD of throwing an error.
18
18
  * Can be used to thrown a custom error OR resolve a promise without throwing.
19
+ *
20
+ * err (which is TimeoutError) is passed as an argument for convenience, so it can
21
+ * be logged or such. You don't have to consume it in any way though.
19
22
  */
20
- onTimeout?: () => any;
23
+ onTimeout?: (err: TimeoutError) => any;
21
24
  /**
22
25
  * Defaults to true.
23
26
  * If true - preserves the stack trace in case of a Timeout (usually - very useful!).
@@ -26,16 +26,18 @@ exports.pTimeoutFn = pTimeoutFn;
26
26
  * If the Function rejects - passes this rejection further.
27
27
  */
28
28
  async function pTimeout(fn, opt) {
29
- // todo: check how we can automatically infer function name (only applicable to named functions)
30
- const { timeout, name, onTimeout, keepStackTrace = true } = opt;
29
+ const { timeout, name = fn.name || 'pTimeout function', onTimeout, keepStackTrace = true } = opt;
31
30
  const fakeError = keepStackTrace ? new Error('TimeoutError') : undefined;
32
31
  // eslint-disable-next-line no-async-promise-executor
33
32
  return await new Promise(async (resolve, reject) => {
34
33
  // Prepare the timeout timer
35
34
  const timer = setTimeout(() => {
35
+ const err = new TimeoutError(`"${name}" timed out after ${timeout} ms`, opt.errorData);
36
+ if (fakeError)
37
+ err.stack = fakeError.stack; // keep original stack
36
38
  if (onTimeout) {
37
39
  try {
38
- resolve(onTimeout());
40
+ resolve(onTimeout(err));
39
41
  }
40
42
  catch (err) {
41
43
  if (fakeError)
@@ -48,9 +50,6 @@ async function pTimeout(fn, opt) {
48
50
  }
49
51
  return;
50
52
  }
51
- const err = new TimeoutError(`"${name || 'pTimeout function'}" timed out after ${timeout} ms`, opt.errorData);
52
- if (fakeError)
53
- err.stack = fakeError.stack; // keep original stack
54
53
  reject(err);
55
54
  }, timeout);
56
55
  // Execute the Function
@@ -21,16 +21,18 @@ export function pTimeoutFn(fn, opt) {
21
21
  * If the Function rejects - passes this rejection further.
22
22
  */
23
23
  export async function pTimeout(fn, opt) {
24
- // todo: check how we can automatically infer function name (only applicable to named functions)
25
- const { timeout, name, onTimeout, keepStackTrace = true } = opt;
24
+ const { timeout, name = fn.name || 'pTimeout function', onTimeout, keepStackTrace = true } = opt;
26
25
  const fakeError = keepStackTrace ? new Error('TimeoutError') : undefined;
27
26
  // eslint-disable-next-line no-async-promise-executor
28
27
  return await new Promise(async (resolve, reject) => {
29
28
  // Prepare the timeout timer
30
29
  const timer = setTimeout(() => {
30
+ const err = new TimeoutError(`"${name}" timed out after ${timeout} ms`, opt.errorData);
31
+ if (fakeError)
32
+ err.stack = fakeError.stack; // keep original stack
31
33
  if (onTimeout) {
32
34
  try {
33
- resolve(onTimeout());
35
+ resolve(onTimeout(err));
34
36
  }
35
37
  catch (err) {
36
38
  if (fakeError)
@@ -40,9 +42,6 @@ export async function pTimeout(fn, opt) {
40
42
  }
41
43
  return;
42
44
  }
43
- const err = new TimeoutError(`"${name || 'pTimeout function'}" timed out after ${timeout} ms`, opt.errorData);
44
- if (fakeError)
45
- err.stack = fakeError.stack; // keep original stack
46
45
  reject(err);
47
46
  }, timeout);
48
47
  // Execute the Function
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@naturalcycles/js-lib",
3
- "version": "14.116.0",
3
+ "version": "14.117.0",
4
4
  "scripts": {
5
5
  "prepare": "husky install",
6
6
  "build-prod": "build-prod-esm-cjs",
@@ -19,8 +19,11 @@ export interface PTimeoutOptions {
19
19
  /**
20
20
  * If provided - will be called INSTEAD of throwing an error.
21
21
  * Can be used to thrown a custom error OR resolve a promise without throwing.
22
+ *
23
+ * err (which is TimeoutError) is passed as an argument for convenience, so it can
24
+ * be logged or such. You don't have to consume it in any way though.
22
25
  */
23
- onTimeout?: () => any
26
+ onTimeout?: (err: TimeoutError) => any
24
27
 
25
28
  /**
26
29
  * Defaults to true.
@@ -59,17 +62,19 @@ export function pTimeoutFn<T extends AnyAsyncFunction>(fn: T, opt: PTimeoutOptio
59
62
  * If the Function rejects - passes this rejection further.
60
63
  */
61
64
  export async function pTimeout<T>(fn: AnyAsyncFunction<T>, opt: PTimeoutOptions): Promise<T> {
62
- // todo: check how we can automatically infer function name (only applicable to named functions)
63
- const { timeout, name, onTimeout, keepStackTrace = true } = opt
65
+ const { timeout, name = fn.name || 'pTimeout function', onTimeout, keepStackTrace = true } = opt
64
66
  const fakeError = keepStackTrace ? new Error('TimeoutError') : undefined
65
67
 
66
68
  // eslint-disable-next-line no-async-promise-executor
67
69
  return await new Promise(async (resolve, reject) => {
68
70
  // Prepare the timeout timer
69
71
  const timer = setTimeout(() => {
72
+ const err = new TimeoutError(`"${name}" timed out after ${timeout} ms`, opt.errorData)
73
+ if (fakeError) err.stack = fakeError.stack // keep original stack
74
+
70
75
  if (onTimeout) {
71
76
  try {
72
- resolve(onTimeout())
77
+ resolve(onTimeout(err))
73
78
  } catch (err: any) {
74
79
  if (fakeError) err.stack = fakeError.stack // keep original stack
75
80
  err.data = {
@@ -81,11 +86,6 @@ export async function pTimeout<T>(fn: AnyAsyncFunction<T>, opt: PTimeoutOptions)
81
86
  return
82
87
  }
83
88
 
84
- const err = new TimeoutError(
85
- `"${name || 'pTimeout function'}" timed out after ${timeout} ms`,
86
- opt.errorData,
87
- )
88
- if (fakeError) err.stack = fakeError.stack // keep original stack
89
89
  reject(err)
90
90
  }, timeout)
91
91