@creejs/commons-lang 2.1.12 → 2.1.13

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.
@@ -3,13 +3,9 @@
3
3
  * 1. timeout=-1, it means no timeout check
4
4
  * @param {number} [timeout=-1] - Timeout duration in milliseconds
5
5
  * @param {string} [timeoutMessage]
6
- * @returns {{promise: Promise<*>, reject: function, resolve: function}}
6
+ * @returns {Deferred}
7
7
  */
8
- export function defer(timeout?: number, timeoutMessage?: string): {
9
- promise: Promise<any>;
10
- reject: Function;
11
- resolve: Function;
12
- };
8
+ export function defer(timeout?: number, timeoutMessage?: string): Deferred;
13
9
  /**
14
10
  * Creates a timeout wrapper around a promise that rejects if the promise doesn't resolve within the given time.
15
11
  * @param {Promise<*>} promise - The promise to wrap with a timeout
@@ -29,13 +25,13 @@ export function timeout(promise: Promise<any>, time?: number, message?: string):
29
25
  * 2. It's NOT convenient to use Promise.allSettled() to get the results of all promises.
30
26
  * * the data structure is not consistent when fullfilled or rejected
31
27
  * * have to check "string" type of status to know sucess or failure
32
- * @param {Promise} promises
33
- * @returns {Array<{ok: boolean, result: any}>}
28
+ * @param {Promise<*>[]} promises
29
+ * @returns {Promise<{ok: boolean, result: any}[]>}
34
30
  */
35
- export function allSettled(promises: Promise<any>): Array<{
31
+ export function allSettled(promises: Promise<any>[]): Promise<{
36
32
  ok: boolean;
37
33
  result: any;
38
- }>;
34
+ }[]>;
39
35
  /**
40
36
  * Execute the task Function, and ensure it returns a Promise.
41
37
  * @param {function} task
@@ -51,7 +47,7 @@ export function returnValuePromised(task: Function): Promise<any>;
51
47
  *
52
48
  * @param {Promise<*>|number|undefined} [promise] - The input promise to delay
53
49
  * @param {number|undefined} [ms] - Minimum delay in milliseconds (default: 1)
54
- * @returns {Promise} A new promise that settles after the delay period
50
+ * @returns {Promise<*>} A new promise that settles after the delay period
55
51
  */
56
52
  export function delay(promise?: Promise<any> | number | undefined, ms?: number | undefined): Promise<any>;
57
53
  /**
@@ -59,10 +55,10 @@ export function delay(promise?: Promise<any> | number | undefined, ms?: number |
59
55
  * 1. export function are executed one by one
60
56
  * 2. Fast Fail: if any tasks fail, the whole chain is rejected with the first error
61
57
  * 3. if an element is not function, rejects the whole chain with Error(Not Function)
62
- * @param {Function[]} promises
63
- * @returns {Promise<Array>} Promise that resolves with an array of results in the same order as input tasks
58
+ * @param {Function[]} tasks
59
+ * @returns {Promise<any[]>} Promise that resolves with an array of results in the same order as input tasks
64
60
  */
65
- export function series(tasks: any): Promise<any[]>;
61
+ export function series(tasks: Function[]): Promise<any[]>;
66
62
  /**
67
63
  * AllSettled Mode to execute Tasks(functions) in series (one after another) and returns their results in order.
68
64
  * 1. tasks are executed one by one
@@ -81,7 +77,7 @@ export function seriesAllSettled(tasks: Function[]): Promise<Array<{
81
77
  * 2. rejects whole chain with the first error, when first task fails
82
78
  * @param {Function[]} tasks
83
79
  * @param {number} [maxParallel=5]
84
- * @returns {Promise<Array>} Array of resolved values from all promises
80
+ * @returns {Promise<any[]>} Array of resolved values from all promises
85
81
  * @throws {TypeError} If input is not an array of export function or maxParallel is not a number
86
82
  */
87
83
  export function parallel(tasks: Function[], maxParallel?: number): Promise<any[]>;
@@ -91,7 +87,7 @@ export function parallel(tasks: Function[], maxParallel?: number): Promise<any[]
91
87
  * 2. all tasks will be executed, even some of them failed.
92
88
  * @param {Function[]} tasks
93
89
  * @param {number} [maxParallel=5] - Maximum number of tasks to run in parallel
94
- * @returns {Promise<Array>} Array of resolved values from all promises
90
+ * @returns {Promise<any[]>} Array of resolved values from all promises
95
91
  * @throws {TypeError} If input is not an array of export function or maxParallel is not a number
96
92
  */
97
93
  export function parallelAllSettled(tasks: Function[], maxParallel?: number): Promise<any[]>;
@@ -107,3 +103,13 @@ declare namespace _default {
107
103
  export { parallelAllSettled };
108
104
  }
109
105
  export default _default;
106
+ export type Deferred = {
107
+ promise: Promise<any>;
108
+ timerHandler: NodeJS.Timeout;
109
+ timerCleared: boolean;
110
+ resolved: boolean;
111
+ rejected: boolean;
112
+ canceled: boolean;
113
+ reject: (reason: Error) => void;
114
+ resolve: (...args: any[]) => void;
115
+ };