Skip to content

Repository files navigation

Timer v^1.0.5

Add a timer to a promise by wrapping it.

Install

$ npm i @anlerandy/timer

Usage

Promise

consttask=require('./my_asynchronous_function');constTimer=require('@anlerandy/timer');constpromise=task();constresult=awaitnewTimer(60000).launchTimer(promise);
Instead of using a `setTimeout` code.
consttask=require('./my_asynchronous_function');constpromise=task();constresult=awaitnewPromise(function(resolve,reject){constid=setTimeout(reject,120000,'Timeout');try{constresult=awaitpromise;resolve(result);}catch(error){reject(error);}clearTimeout(id);})

Callback

consttask=require('./my_function');constTimer=require('@anlerandy/timer');consttimer=newTimer();task(callback);launchTimer(onFailure);// Launch clock and calls onFailure if time runs outfunctioncallback(error){timer.done();// Stop the clock without calling onFailureif(error){returnonFailure();}// Do Something}functiononFailure(){// 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.

consttimer=Timer.getById('myTaskId');awaittimer.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.

PropertyTypeDefaultDescription
_idStringN/Aid of the instance
createdAtDateN/ACreation date
startedAtDateundefinedLaunch date
lastUpdateDatecreatedAtLast update (i.e. last postpone)
inProgressbooleanfalseTrue if running
isAbortedbooleanfalseTrue if cancelled
isSelfAbortedbooleanfalseTrue if the timer cancelled its task
timeNumber120000Time to wait before timeout (ms)

All properties are undefined if the instance is being deleted.

consttimer=newTimer();timer.destroy();constdate=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.

About

Timeout manager for sensible process.

Topics

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages