Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/maoberlehner/retry-until-online
Delay the execution of a function until the browser is back online.
https://github.com/maoberlehner/retry-until-online
Last synced: 16 days ago
JSON representation
Delay the execution of a function until the browser is back online.
- Host: GitHub
- URL: https://github.com/maoberlehner/retry-until-online
- Owner: maoberlehner
- License: mit
- Created: 2016-09-30T15:02:33.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2018-05-20T14:19:57.000Z (over 6 years ago)
- Last Synced: 2024-10-22T13:28:47.671Z (22 days ago)
- Language: JavaScript
- Homepage:
- Size: 241 KB
- Stars: 5
- Watchers: 3
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# retry-until-online
[![Patreon](https://img.shields.io/badge/patreon-donate-blue.svg)](https://www.patreon.com/maoberlehner)
[![Donate](https://img.shields.io/badge/Donate-PayPal-blue.svg)](https://paypal.me/maoberlehner)
[![Build Status](https://travis-ci.org/maoberlehner/retry-until-online.svg?branch=master)](https://travis-ci.org/maoberlehner/retry-until-online)
[![Coverage Status](https://coveralls.io/repos/github/maoberlehner/retry-until-online/badge.svg?branch=master)](https://coveralls.io/github/maoberlehner/retry-until-online?branch=master)
[![GitHub stars](https://img.shields.io/github/stars/maoberlehner/retry-until-online.svg?style=social&label=Star)](https://github.com/maoberlehner/retry-until-online)Delay the execution of a function until the browser is back online.
Imagine a user is traveling by train and browsing your webshop on his phone. He finds what he is looking for and taps the "Buy now" button. But suddenly: a tunnel. The connection is lost and the AJAX request (for putting the product in the cart) fails.
By wrapping AJAX requests with the `retryUntilOnline` function you can prevent failing AJAX requests. Like in the tunnel example, the connection may be only lost for some seconds `retryUntilOnline` will execute the AJAX request as soon as the connection is restored.
## Usage
```bash
# Install the module.
npm install retry-until-online --save
``````js
// Load the module.
import retryUntilOnline from 'retry-until-online';// Delay the execution of a jQuery AJAX request until the browser is back online.
retryUntilOnline({ callback: () => $.ajax({ url: 'test.html' }) })
.then((value) => {
// This is called after the callback function was called,
// `value` is the return value of the callback function.
})
.catch((value) => {
// This is called when `maxTries` is reached and the offline
// callback function was called, `value` is the return value
// of the offline callback function.
});
```### Options
```js
import retryUntilOnline from 'retry-until-online';const options = {
callback: () => undefined, // Callback function which is called if online.
offlineCallback: () => undefined, // Callback function that is executed if `tries` reaches 0.
interval: 500, // Interval for checking the online status.
maxTries: -1, // How often the online status should be checked before giving up (-1 = unlimited).
};retryUntilOnline(options);
```## About
### Author
Markus Oberlehner
Website: https://markus.oberlehner.net
Twitter: https://twitter.com/MaOberlehner
PayPal.me: https://paypal.me/maoberlehner
Patreon: https://www.patreon.com/maoberlehner### License
MIT