get-promisable-result 1.0.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 +76 -0
- package/dist/esm/index.d.ts +10 -0
- package/dist/esm/index.js +1 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +1 -0
- package/package.json +66 -0
package/README.md
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
# get-promisable-result
|
|
2
|
+
|
|
3
|
+
A very small JavaScript utility to check and retry a function a limited number of times abstracting it in a Promise
|
|
4
|
+
|
|
5
|
+
[](https://github.com/elchininet/get-promisable-result/actions/workflows/deploy.yaml)
|
|
6
|
+
[](https://github.com/elchininet/get-promisable-result/actions/workflows/tests.yaml)
|
|
7
|
+
[](https://coveralls.io/github/elchininet/get-promisable-result?branch=master)
|
|
8
|
+
[](https://badge.fury.io/js/get-promisable-result)
|
|
9
|
+
|
|
10
|
+
## Install
|
|
11
|
+
|
|
12
|
+
#### npm
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
npm install get-promisable-result
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
#### yarn
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
yarn add get-promisable-result
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
#### PNPM
|
|
25
|
+
|
|
26
|
+
```bash
|
|
27
|
+
pnpm add get-promisable-result
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## API
|
|
31
|
+
|
|
32
|
+
```typescript
|
|
33
|
+
getPromisableResult(
|
|
34
|
+
getResultFunction: () => T,
|
|
35
|
+
checkResult: (result: T) => boolean,
|
|
36
|
+
options: PromisableOptions = {}
|
|
37
|
+
): Promise<T>
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
`getResultFunction` should be a function that returns something. This something is what you expect when the promise gets resolved.
|
|
41
|
+
|
|
42
|
+
`checkResult` is a function that will have as a parameter the result of the `getResultFunction` and it should return a `boolean` to validate that the result is OK. If this function returns `false`, the `getResultFunction` will be executed a limited number of times (consult the [options](#options) section) until its result pass the check or until it reached the `retries` (consult the [options](#options) section).
|
|
43
|
+
|
|
44
|
+
### Options
|
|
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` |
|
|
52
|
+
|
|
53
|
+
>\*\* The default `rejectMessage` will be `Could not get the result after {{ retries }} retries`
|
|
54
|
+
|
|
55
|
+
## Example
|
|
56
|
+
|
|
57
|
+
Querying a DOM element that is rendered asynchronously with a custom `retries`, `delay` and `rejectMessage`
|
|
58
|
+
|
|
59
|
+
```typescript
|
|
60
|
+
getPromisableResult(
|
|
61
|
+
() => document.getElementById('my-element'),
|
|
62
|
+
(element) => element !== null,
|
|
63
|
+
{
|
|
64
|
+
retries: 50,
|
|
65
|
+
delay: 100,
|
|
66
|
+
rejectMessage: 'My element could not be retrieved after {{ restries }} retries'
|
|
67
|
+
}
|
|
68
|
+
)
|
|
69
|
+
.then((element) => {
|
|
70
|
+
// Do something with the element
|
|
71
|
+
})
|
|
72
|
+
.catch((error) => {
|
|
73
|
+
// Do something if the promise is rejected
|
|
74
|
+
// My element could not be retrieved after 50 retries
|
|
75
|
+
});
|
|
76
|
+
```
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
declare const DEFAULT_RETRIES = 10;
|
|
2
|
+
declare const DEFAULT_DELAY = 10;
|
|
3
|
+
interface PromisableOptions {
|
|
4
|
+
retries?: number;
|
|
5
|
+
delay?: number;
|
|
6
|
+
shouldReject?: boolean;
|
|
7
|
+
rejectMessage?: string;
|
|
8
|
+
}
|
|
9
|
+
declare const getPromisableResult: <T>(getResultFunction: () => T, checkResult: (result: T) => boolean, options?: PromisableOptions) => Promise<T>;
|
|
10
|
+
export { DEFAULT_RETRIES, DEFAULT_DELAY, getPromisableResult };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
var e=10,r=10,t=function(e,r,t){var o;void 0===t&&(t={});var i=t.retries,n=void 0===i?10:i,s=t.delay,a=void 0===s?10:s,v=t.shouldReject,u=void 0===v||v,c=null!==(o=t.rejectMessage)&&void 0!==o?o:"Could not get the result after {{ retries }} retries";return new Promise((function(t,o){var i=0,s=function(){var v=e();r(v)?t(v):++i<n?setTimeout(s,a):u?o(new Error(c.replace(/\{\{\s*retries\s*\}\}/g,"".concat(n)))):t(v)};s()}))};export{r as DEFAULT_DELAY,e as DEFAULT_RETRIES,t as getPromisableResult};
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
declare const DEFAULT_RETRIES = 10;
|
|
2
|
+
declare const DEFAULT_DELAY = 10;
|
|
3
|
+
interface PromisableOptions {
|
|
4
|
+
retries?: number;
|
|
5
|
+
delay?: number;
|
|
6
|
+
shouldReject?: boolean;
|
|
7
|
+
rejectMessage?: string;
|
|
8
|
+
}
|
|
9
|
+
declare const getPromisableResult: <T>(getResultFunction: () => T, checkResult: (result: T) => boolean, options?: PromisableOptions) => Promise<T>;
|
|
10
|
+
export { DEFAULT_RETRIES, DEFAULT_DELAY, getPromisableResult };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";exports.DEFAULT_DELAY=10,exports.DEFAULT_RETRIES=10,exports.getPromisableResult=function(e,r,t){var o;void 0===t&&(t={});var s=t.retries,i=void 0===s?10:s,n=t.delay,u=void 0===n?10:n,a=t.shouldReject,c=void 0===a||a,l=null!==(o=t.rejectMessage)&&void 0!==o?o:"Could not get the result after {{ retries }} retries";return new Promise((function(t,o){var s=0,n=function(){var a=e();r(a)?t(a):++s<i?setTimeout(n,u):c?o(new Error(l.replace(/\{\{\s*retries\s*\}\}/g,"".concat(i)))):t(a)};n()}))};
|
package/package.json
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "get-promisable-result",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "A very small JavaScript utility to check and retry a function a limited number of times abstracting it in a Promise",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"promise",
|
|
7
|
+
"promisable",
|
|
8
|
+
"async",
|
|
9
|
+
"promisable-result",
|
|
10
|
+
"javascript",
|
|
11
|
+
"utility",
|
|
12
|
+
"javascript-utility",
|
|
13
|
+
"retry",
|
|
14
|
+
"check-and-retry"
|
|
15
|
+
],
|
|
16
|
+
"main": "dist/index.js",
|
|
17
|
+
"module": "dist/esm/index.js",
|
|
18
|
+
"types": "dist/index.d.ts",
|
|
19
|
+
"exports": {
|
|
20
|
+
".": {
|
|
21
|
+
"require": {
|
|
22
|
+
"types": "./dist/index.d.ts",
|
|
23
|
+
"default": "./dist/index.js"
|
|
24
|
+
},
|
|
25
|
+
"import": {
|
|
26
|
+
"types": "./dist/esm/index.d.ts",
|
|
27
|
+
"default": "./dist/esm/index.js"
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
},
|
|
31
|
+
"files": [
|
|
32
|
+
"dist/**/*"
|
|
33
|
+
],
|
|
34
|
+
"author": "ElChiniNet",
|
|
35
|
+
"license": "Apache-2.0",
|
|
36
|
+
"repository": {
|
|
37
|
+
"type": "git",
|
|
38
|
+
"url": "git+https://github.com/elchininet/get-promisable-result"
|
|
39
|
+
},
|
|
40
|
+
"scripts": {
|
|
41
|
+
"build": "rollup --config rollup.config.js --bundleConfigAsCjs",
|
|
42
|
+
"test:ts": "tsc --noEmit",
|
|
43
|
+
"test:lint": "eslint \"src/**/*.ts\" \"tests/**/*.ts\"",
|
|
44
|
+
"test:unit": "jest --clearCache && jest --verbose --coverage",
|
|
45
|
+
"test:all": "pnpm test:ts && pnpm test:lint && pnpm test:unit",
|
|
46
|
+
"prepare": "pnpm build",
|
|
47
|
+
"prepublishOnly": "pnpm test:all",
|
|
48
|
+
"version": "git add .",
|
|
49
|
+
"postversion": "git push && git push --tags"
|
|
50
|
+
},
|
|
51
|
+
"devDependencies": {
|
|
52
|
+
"@rollup/plugin-terser": "^0.4.4",
|
|
53
|
+
"@types/eslint": "^9.6.1",
|
|
54
|
+
"@types/jest": "^29.5.14",
|
|
55
|
+
"@types/node": "^22.5.4",
|
|
56
|
+
"eslint": "^9.10.0",
|
|
57
|
+
"globals": "^15.9.0",
|
|
58
|
+
"jest": "^29.7.0",
|
|
59
|
+
"rollup": "4.21.2",
|
|
60
|
+
"rollup-plugin-ts": "^3.4.5",
|
|
61
|
+
"ts-jest": "^29.2.5",
|
|
62
|
+
"tslib": "^2.7.0",
|
|
63
|
+
"typescript": "^5.6.2",
|
|
64
|
+
"typescript-eslint": "^8.4.0"
|
|
65
|
+
}
|
|
66
|
+
}
|