Skip to content

Commit

Permalink
feat: promise with retry strategy package
Browse files Browse the repository at this point in the history
  • Loading branch information
Jordy Cabannes committed Aug 22, 2023
1 parent d86e80b commit ad9249f
Show file tree
Hide file tree
Showing 7 changed files with 472 additions and 0 deletions.
62 changes: 62 additions & 0 deletions packages/retry-promise/README.md
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)
15 changes: 15 additions & 0 deletions packages/retry-promise/jest.config.js
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
}
},
}
45 changes: 45 additions & 0 deletions packages/retry-promise/package.json
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"
}
}
3 changes: 3 additions & 0 deletions packages/retry-promise/rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import config from '../../rollup-template.js'

export default config([])
Loading

0 comments on commit ad9249f

Please sign in to comment.