https://github.com/mrloop/ember-retry
Retry function / promise with configurable backoff which defaults to exponential
https://github.com/mrloop/ember-retry
Last synced: about 1 year ago
JSON representation
Retry function / promise with configurable backoff which defaults to exponential
- Host: GitHub
- URL: https://github.com/mrloop/ember-retry
- Owner: mrloop
- License: mit
- Created: 2016-03-21T13:48:58.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2021-09-21T05:01:07.000Z (almost 5 years ago)
- Last Synced: 2024-01-04T17:46:11.481Z (over 2 years ago)
- Language: JavaScript
- Homepage:
- Size: 2.91 MB
- Stars: 7
- Watchers: 1
- Forks: 6
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE.md
Awesome Lists containing this project
README

ember-retry
==============================================================================
[Ember](https://ember-cli.com) addon for exponetial backoff retries of a function.
Compatibility
------------------------------------------------------------------------------
* Ember.js v3.16 or above
* Ember CLI v2.13 or above
* Node.js v10 or above
Installation
------------------------------------------------------------------------------
```
ember install ember-retry
```
Usage
------------------------------------------------------------------------------
```javascript
import retry from 'ember-retry/retry'
retry((resolve, reject) => { //retry
let ws = new WebSocket('ws://myflakyhost.com');
ws.onopen = () => resolve(ws);
ws.onerror = (error) => reject(error);
}).then((websocket) => {
//do something with websocket
});
retry(() => {
return this.store.find('user', 353232); //retry if promise fails with error
}).then((user) => {
//do something with user
});
```
By default will retry 5 times after 0.5s, 1s, 2s, 4s, 8s.
The number of retries, initial delay before retries and the function used to calculate retry delay can all be configured.
```javascript
retry(() => {
return this.store.find('user', 353232);
}, 3, 1000) //retry 3 times at 1000ms, 2000ms, 4000ms
```
```javascript
retry(() => {
return this.store.find('user', 353232);
}, 5, (retryIndex) => { return retryIndex+1*40; }); //retry 5 times at 40ms, 80ms, 120ms, 160ms, 200ms
```
Will retry if return value `isNone`
```javascript
retry(() => {
if (Math.random() > 0.5) {
return 'What am I trying to do?';
}
}).then((str) => {
// str is 'What am I trying to do?' if successful;
})
```
Can pass an anonymous function to retry conditionally
In this case if the response.status is not 401
```javascript
retry(() => {
return this.store.find('user', 353232);
}, 5, 1000, (response) => { return (response.status !== 401) });
```
Contributing
------------------------------------------------------------------------------
See the [Contributing](CONTRIBUTING.md) guide for details.
Alternatives
------------------------------------------------------------------------------
_Why don't you use existing alternative instead e.g. ember-backoff?_
I wanted a cleaner way to retry a function where a promise wasn't returned.
* [ember-backoff](https://github.com/GavinJoyce/ember-backoff)
* [ember-concurrency](https://github.com/machty/ember-concurrency) like [this](http://blog.mrloop.com/javascript/ember/2016/04/12/retrying-functions-with-ember-concurrency.html)