timer-tools
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

Timer-tools

Timers utils. In case you don't want to mess with setTimeout yourself.

Install

npm install timer-tools

Functions

Wait

Wait time.

const { wait } = require("timer-tools");

(async () => {
  console.log("Time before", new Date());
  await wait(1000);
  console.log("Time after", new Date());
})();

waitInfinity (waitIndefinitely)

Wait indefinitely.

const { waitInfinity } = require("timer-tools");

(async () => {
  await waitInfinity();
  console.log("this code will never be executed");
})();

waitUntil

Wait until the condition is met (can't stop it manually).

const { waitUntil } = require("timer-tools");

(async () => {
  const currentTime = +new Date();
  await waitUntil(() => +new Date() - currentTime > 1000, 50);
  console.log("End");
})();
const { waitUntil } = require("timer-tools");

(async () => {
  try {
    await waitUntil(
      () => {
        return false;
      },
      1000, // waiting between executions
      5 // max tries
    );
  } catch (e) {
    console.warn(
      "the comparison went through 5 times and caused an exceeded execution attempt exception"
    );
  }
})();

setIntervalAsync

Execute the specified asynchronous function at the specified interval and limiting the maximum number of calls.

const { setIntervalAsync } = require("timer-tools");

setIntervalAsync(
  () => {
    console.log(new Date());
  },
  1000, // time between executions
  5 // max count of executions
);

Package Sidebar

Install

npm i timer-tools

Weekly Downloads

1

Version

1.0.0

License

MIT

Unpacked Size

12.3 kB

Total Files

7

Last publish

Collaborators

  • carcinogen75