get-promisable-result 2.0.0 → 2.1.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.
package/README.md CHANGED
@@ -43,16 +43,17 @@ getPromisableResult(
43
43
 
44
44
  ### Options
45
45
 
46
- | Parameter | Type | Default | Description |
47
- | -------------- | ------------- | ------- | ------------------------------------------------------------------- |
48
- | retries | number | 10 | number of retries that will be performed before the promise rejects |
49
- | delay | number | 10 | delay between the retries |
50
- | shouldReject | boolean | true | indicates if the promise should be rejected when the number of retries reached the limit. If this parameter is set to `false` and the number of retries is reached the Promise will be resolved with the last value returned by `getResultFunction` |
51
- | rejectMessage | string | \*\* | custom error message. It can contain the substring `{{ retries }}` and it will be replaced with the number of `retries` |
46
+ | Parameter | Type | Default | Description |
47
+ | -------------- | ------------- | --------- | ------------------------------------------------------------------- |
48
+ | retries | number | 10 | number of retries that will be performed before the promise rejects |
49
+ | delay | number | 10 | delay between the retries |
50
+ | shouldReject | boolean | true | indicates if the promise should be rejected when the number of retries reached the limit. If this parameter is set to `false` and the number of retries is reached the Promise will be resolved with the last value returned by `getResultFunction` |
51
+ | rejectMessage | string | \*\* | custom error message. It can contain the substring `{{ retries }}` and it will be replaced with the number of `retries` |
52
+ | signal | AbortSignal | undefined | An [AbortSignal](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal) object that allows you abort the promise before it is fullfilled if required via an [AbortController instance](https://developer.mozilla.org/en-US/docs/Web/API/AbortController) |
52
53
 
53
54
  >\*\* The default `rejectMessage` will be `Could not get the result after {{ retries }} retries`
54
55
 
55
- ## Example
56
+ ## Examples
56
57
 
57
58
  Querying a DOM element that is rendered asynchronously with a custom `retries`, `delay` and `rejectMessage`
58
59
 
@@ -73,4 +74,36 @@ getPromisableResult(
73
74
  // Do something if the promise is rejected
74
75
  // My element could not be retrieved after 50 retries
75
76
  });
77
+ ```
78
+
79
+ Querying a DOM element that is rendered asynchronously with the ability to abort the query
80
+
81
+ ```typescript
82
+ const controller = new AbortController();
83
+ const { signal } = controller;
84
+
85
+ getPromisableResult(
86
+ () => document.getElementById('my-element'),
87
+ (element) => element !== null,
88
+ {
89
+ retries: 2000,
90
+ delay: 100,
91
+ rejectMessage: 'My element could not be retrieved after {{ restries }} retries',
92
+ signal
93
+ }
94
+ )
95
+ .then((element) => {
96
+ // Do something with the element
97
+ })
98
+ .catch((error) => {
99
+ if (error.name === 'AbortError') {
100
+ // Do something if the promise was aborted on purpose
101
+ } else {
102
+ // Do something if the promise is rejected
103
+ // My element could not be retrieved after 50 retries
104
+ }
105
+ });
106
+
107
+ // Abort the promise on purpose
108
+ controller.abort();
76
109
  ```
@@ -3,6 +3,7 @@ interface PromisableOptions {
3
3
  delay?: number;
4
4
  shouldReject?: boolean;
5
5
  rejectMessage?: string;
6
+ signal?: AbortSignal;
6
7
  }
7
8
  declare const getPromisableResult: <T>(getResultFunction: () => T, checkResult: (result: T) => boolean, options?: PromisableOptions) => Promise<T>;
8
9
 
package/dist/esm/index.js CHANGED
@@ -1 +1 @@
1
- const e=10,r=10,t=(t,s,o={})=>{var n;const{retries:l=e,delay:c=r,shouldReject:i=!0}=o,u=null!==(n=o.rejectMessage)&&void 0!==n?n:"Could not get the result after {{ retries }} retries";return new Promise((e,r)=>{let o=0;const n=()=>{const a=t();s(a)?e(a):(o++,o<l?setTimeout(n,c):i?r(new Error(u.replace(/\{\{\s*retries\s*\}\}/g,`${l}`))):e(a))};n()})};export{t as getPromisableResult};
1
+ const e=10,r=10,t=(t,s,o={})=>{var n;const{retries:l=e,delay:a=r,shouldReject:i=!0,signal:c}=o,u=null!==(n=o.rejectMessage)&&void 0!==n?n:"Could not get the result after {{ retries }} retries";return new Promise((e,r)=>{let o=0;const n=()=>{const c=t();s(c)?e(c):(o++,o<l?setTimeout(n,a):i?r(new Error(u.replace(/\{\{\s*retries\s*\}\}/g,`${l}`))):e(c))};null==c||c.throwIfAborted(),null==c||c.addEventListener("abort",()=>{r(c.reason)},{once:!0}),n()})};export{t as getPromisableResult};
package/dist/index.d.ts CHANGED
@@ -3,6 +3,7 @@ interface PromisableOptions {
3
3
  delay?: number;
4
4
  shouldReject?: boolean;
5
5
  rejectMessage?: string;
6
+ signal?: AbortSignal;
6
7
  }
7
8
  declare const getPromisableResult: <T>(getResultFunction: () => T, checkResult: (result: T) => boolean, options?: PromisableOptions) => Promise<T>;
8
9
 
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- "use strict";const e=10,t=10;exports.getPromisableResult=(r,s,o={})=>{var l;const{retries:i=e,delay:n=t,shouldReject:c=!0}=o,u=null!==(l=o.rejectMessage)&&void 0!==l?l:"Could not get the result after {{ retries }} retries";return new Promise((e,t)=>{let o=0;const l=()=>{const a=r();s(a)?e(a):(o++,o<i?setTimeout(l,n):c?t(new Error(u.replace(/\{\{\s*retries\s*\}\}/g,`${i}`))):e(a))};l()})};
1
+ "use strict";const e=10,t=10;exports.getPromisableResult=(r,s,o={})=>{var n;const{retries:l=e,delay:i=t,shouldReject:a=!0,signal:u}=o,c=null!==(n=o.rejectMessage)&&void 0!==n?n:"Could not get the result after {{ retries }} retries";return new Promise((e,t)=>{let o=0;const n=()=>{const u=r();s(u)?e(u):(o++,o<l?setTimeout(n,i):a?t(new Error(c.replace(/\{\{\s*retries\s*\}\}/g,`${l}`))):e(u))};null==u||u.throwIfAborted(),null==u||u.addEventListener("abort",()=>{t(u.reason)},{once:!0}),n()})};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "get-promisable-result",
3
- "version": "2.0.0",
3
+ "version": "2.1.0",
4
4
  "description": "A very small JavaScript utility to check and retry a function a limited number of times abstracting it in a Promise",
5
5
  "keywords": [
6
6
  "promise",
@@ -13,6 +13,7 @@
13
13
  "retry",
14
14
  "check-and-retry"
15
15
  ],
16
+ "type": "module",
16
17
  "main": "dist/index.js",
17
18
  "module": "dist/esm/index.js",
18
19
  "types": "dist/index.d.ts",
@@ -38,7 +39,7 @@
38
39
  "url": "git+https://github.com/elchininet/get-promisable-result"
39
40
  },
40
41
  "scripts": {
41
- "build": "rollup --config rollup.config.js --bundleConfigAsCjs",
42
+ "build": "rollup --config rollup.config.mjs",
42
43
  "test:ts": "tsc --noEmit",
43
44
  "test:lint": "eslint \"src/**/*.ts\" \"tests/**/*.ts\"",
44
45
  "test:unit": "jest --clearCache && jest --verbose --coverage",
@@ -54,21 +55,17 @@
54
55
  "@rollup/plugin-typescript": "^12.3.0",
55
56
  "@types/eslint": "^9.6.1",
56
57
  "@types/jest": "^30.0.0",
57
- "@types/node": "^25.5.0",
58
- "eslint": "^10.1.0",
59
- "globals": "^17.4.0",
60
- "jest": "^30.3.0",
61
- "rollup": "4.60.0",
62
- "rollup-plugin-dts": "^6.4.1",
58
+ "@types/node": "^26.2.0",
59
+ "eslint": "^10.9.0",
60
+ "globals": "^17.11.0",
61
+ "jest": "^30.4.2",
62
+ "rollup": "4.62.5",
63
+ "rollup-plugin-dts": "^6.5.1",
63
64
  "rollup-plugin-tsconfig-paths": "^1.5.2",
64
- "ts-jest": "^29.4.6",
65
+ "ts-jest": "^29.4.12",
65
66
  "tslib": "^2.7.0",
66
- "typescript": "^6.0.2",
67
- "typescript-eslint": "^8.57.2"
67
+ "typescript": "^6.0.3",
68
+ "typescript-eslint": "^8.67.0"
68
69
  },
69
- "pnpm": {
70
- "overrides": {
71
- "@isaacs/brace-expansion@<=5.0.0": ">=5.0.1"
72
- }
73
- }
70
+ "packageManager": "pnpm@11.15.0"
74
71
  }