periodic-execution

0.1.0 • Public • Published

node-periodic-execution

npm version Node.js CI

A helper that periodically executes a function until a timeout.

node-periodic-execution is a simple node.js function that executes a passed function either until a this function 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

$ npm i --save periodic-execution

Usage

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 () => {
  await periodicExecution(fn, 200, timeout, { interval: 100 });

  const expectedStatus = 500;
  try {
    // NOTE: Let's check if google.com's server throws a 500 status.
    await periodicExecution(fn2, expectedStatus, timeout, { interval: 100 });
  } 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.

Package Sidebar

Install

npm i periodic-execution

Weekly Downloads

0

Version

0.1.0

License

GPL-3.0-only

Unpacked Size

42.2 kB

Total Files

8

Last publish

Collaborators

  • timdaub