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.
node-periodic-execution uses node's process.hrtime
for time-keeping, hence a
node.js environment is required.
$ npm i --save periodic-execution
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`);
}
}
})();
- 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
andperiodicExecution
(see Usage section).
- Initial release
See License.