https://github.com/attestate/node-periodic-execution
A helper function that periodically executes a function until a timeout
https://github.com/attestate/node-periodic-execution
interval nodejs periodic schedule
Last synced: 3 months ago
JSON representation
A helper function that periodically executes a function until a timeout
- Host: GitHub
- URL: https://github.com/attestate/node-periodic-execution
- Owner: attestate
- License: gpl-3.0
- Created: 2021-01-07T16:32:25.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2023-03-31T11:55:19.000Z (about 2 years ago)
- Last Synced: 2024-03-24T01:01:53.484Z (about 1 year ago)
- Topics: interval, nodejs, periodic, schedule
- Language: JavaScript
- Homepage:
- Size: 54.7 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# node-periodic-execution
[](https://badge.fury.io/js/periodic-execution) 
> A helper that periodically executes a function until a timeout.
node-periodic-execution is a simple node.js function that executes a
function either until it returns an expected value or when a
timeout threshold is reached.## Requirements
node-periodic-execution uses node's `process.hrtime` for time-keeping, hence a
node.js environment is required.## Installation
```bash
$ npm i --save periodic-execution
```## Usage
```js
const fetch = require("cross-fetch");
const { periodicExecution, TimeoutError } = require("periodic-execution");const timeout = 1000; // in milliseconds
// Let's check if Google is online
const fn = async () => {
const res = await fetch("https://google.de");
return res.status;
};const fn2 = async () => {
const res = await fetch("https://google.com");
return res.status;
};(async () => {
const expectedOutcome = 200;
const options = { interval: 100 };
await periodicExecution(fn, expectedOutcome, timeout, options);const expectedStatus = 500;
try {
// NOTE: Let's check if google.com's server throws a 500 status.
await periodicExecution(fn2, expectedStatus, timeout, options);
} catch(err) {
if (err instanceof TimeoutError) {
console.info(`Wasn't able to retrieve status ${expectedStatus} from URL`);
}
}
})();
```## Changelog
### 0.1.0
- Remove function default export: `const periodicExecution = require...` won't
work anymore.
- Introduce `TimeoutError`. It's thrown when the timeout threshold has been
reached.
- Export object with properties `TimeoutError` and `periodicExecution` (see
Usage section).### 0.0.1
- Initial release
## License
See [License](./LICENSE).