Git Product home page Git Product logo

timerclass's Introduction

Timer v^1.0.5

Add a timer to a promise by wrapping it.

Install

$ npm i @anlerandy/timer

Usage

Promise

const task = require('./my_asynchronous_function');
const Timer = require('@anlerandy/timer');

const promise = task();
const result = await new Timer(60000).launchTimer(promise);
Instead of using a `setTimeout` code.
  const task = require('./my_asynchronous_function');

  const promise = task();
  const result = await new Promise(function (resolve, reject) {
    const id = setTimeout(reject, 120000, 'Timeout');
    try {
      const result = await promise;
      resolve(result);
    } catch (error) {
      reject(error);
    }
    clearTimeout(id);
  })

Callback

  const task = require('./my_function');
  const Timer = require('@anlerandy/timer');

  const timer = new Timer();

  task(callback);
  launchTimer(onFailure); // Launch clock and calls onFailure if time runs out

  function callback(error) {
    timer.done(); // Stop the clock without calling onFailure
    if (error) {
      return onFailure();
    }
    // Do Something
  }

  function onFailure() {
    // Do Something
  }

Class

new Timer(time?: number, options?: object)

Instanciate a timer set.
time parameter is in millisecond.

Timer.getById(id: string, options?: object)

Return an instance of Timer by id.
Unless options.createOne is true, return undefined if no timer was found.
Could throw if timer creation fails.

  const timer = Timer.getById('myTaskId');
  await timer.launchTimer(task());

Timer.getAll()

Return an array of all saved timer instances.

Timer.destroyAll(force?: boolean)

Delete all saved timer instances that are not running.

Instance properties

Only time property can be set (timer.time = 2000). Will not be updated if value is not a number.
Changing time while Timer is running can result in a timeout.

Property Type Default Description
_id String N/A id of the instance
createdAt Date N/A Creation date
startedAt Date undefined Launch date
lastUpdate Date createdAt Last update (i.e. last postpone)
inProgress boolean false True if running
isAborted boolean false True if cancelled
isSelfAborted boolean false True if the timer cancelled its task
time Number 120000 Time to wait before timeout (ms)

All properties are undefined if the instance is being deleted.

const timer = new Timer();
timer.destroy();
const date = timer.createdAt;
console.log(date);
// output: undefined

Instance API

timer.launchTimer(callback: function | promise, argument?)

Run the timer.
If callback argument is a function, it returns undefined.
If it's a Promise, it will return a Promise.

timer.done()

Terminate timer without triggering callback or promise.reject.
If destroy option was true on instanciation, deletes the timer.

timer.destroy()

Destroy the instance.
Throw an error if instance is not terminated via _.done() or _.abort().
Automaticaly called after termination if destroy option was set as true.

timer.update()

Reset the clock of timer.
Useful if there is multiple task to be done, and each should be under the same timer.

timer.abort()

Run callback or promise.reject. Then, work as timer.done().
Can be used to terminate a task if the said task verify if timer is aborted.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.