forked from linagora/ToM-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: promise with retry strategy package
- Loading branch information
Jordy Cabannes
committed
Aug 22, 2023
1 parent
d86e80b
commit ad9249f
Showing
7 changed files
with
472 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
# @twake/retry-promise | ||
|
||
Simple module extending javascript Promise with retry strategy | ||
|
||
## Synopsis | ||
|
||
```js | ||
import RetryPromise from '@twake/retry-promise' | ||
|
||
const promise = new RetryPromise((resolve, reject) => { | ||
fetch(URL) | ||
.then(val => resolve()) | ||
.catch(reject) | ||
}) | ||
|
||
const promiseWithOption = new RetryPromise((resolve, reject) => { | ||
fetch(URL) | ||
.then(val => resolve()) | ||
.catch(reject) | ||
},{ | ||
retries: 6 | ||
minTimeout: 10 // The number of milliseconds before starting the first retry | ||
maxTimeout: 100 // The maximum number of milliseconds between two retries | ||
} | ||
) | ||
|
||
const allPromises = RetryPromise.all([ | ||
new RetryPromise((resolve, reject) => { | ||
fetch(URL_1) | ||
.then(val => resolve()) | ||
.catch(reject) | ||
}), | ||
new RetryPromise((resolve, reject) => { | ||
fetch(URL_2) | ||
.then(val => resolve()) | ||
.catch(reject) | ||
}) | ||
]) | ||
|
||
const allSettledtPromises = RetryPromise.allSettled([ | ||
new RetryPromise((resolve, reject) => { | ||
fetch(URL_1) | ||
.then(val => resolve()) | ||
.catch(reject) | ||
}), | ||
new RetryPromise((resolve, reject) => { | ||
fetch(URL_2) | ||
.then(val => resolve()) | ||
.catch(reject) | ||
}) | ||
]) | ||
``` | ||
|
||
## How it works | ||
|
||
`RetryPromise` allows to build promises that can be re-triggered 3 times or a custom number of times. We can also redefine `minTimeout` option and `maxTimeout` option values, by default it is 100 ms and 1000 ms. Finally, `all` and `allSettled` are very similar to the native Promise methods, but they do not accept Promises instances as parameters. | ||
|
||
## Copyright and license | ||
|
||
Copyright (c) 2023-present Linagora <https://linagora.com> | ||
|
||
License: [GNU AFFERO GENERAL PUBLIC LICENSE](https://ci.linagora.com/publicgroup/oss/twake/tom-server/-/blob/master/LICENSE) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
export default { | ||
testTimeout: 10000, | ||
testEnvironment: 'node', | ||
preset: 'ts-jest', | ||
collectCoverage: true, | ||
collectCoverageFrom: ['./src/**/*.ts'], | ||
coverageThreshold: { | ||
global: { | ||
branches: 80, | ||
functions: 50, | ||
lines: 90, | ||
statements: 90 | ||
} | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
{ | ||
"name": "@twake/retry-promise", | ||
"version": "0.0.1", | ||
"description": "Promise with retry strategy module", | ||
"main": "dist/index.js", | ||
"type": "module", | ||
"types": "./dist/index.d.ts", | ||
"exports": { | ||
"import": "./dist/index.js" | ||
}, | ||
"scripts": { | ||
"build": "rollup -c", | ||
"test": "jest" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://ci.linagora.com/publicgroup/oss/twake/tom-server.git" | ||
}, | ||
"files": [ | ||
"package.json", | ||
"dist", | ||
"*.md" | ||
], | ||
"keywords": [ | ||
"matrix", | ||
"twake" | ||
], | ||
"author": [ | ||
{ | ||
"name": "Jordy Cabannes", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"license": "AGPL-3.0-or-later", | ||
"bugs": { | ||
"url": "https://ci.linagora.com/publicgroup/oss/twake/tom-server/-/issues" | ||
}, | ||
"homepage": "https://ci.linagora.com/publicgroup/oss/twake/tom-server", | ||
"dependencies": { | ||
"retry": "^0.13.1" | ||
}, | ||
"devDependencies": { | ||
"@types/retry": "^0.12.2" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import config from '../../rollup-template.js' | ||
|
||
export default config([]) |
Oops, something went wrong.