task-handler
A simple, dependency-free task scheduling manager that makes it easy to handle tasks like a boss.
Install
yarn add task-handler
or
npm install --save task-handler
Coverage
Flow
This project provides .flow.js
files for Flow
to utilize. It also attempts to provide near-100% test coverage.
Typescript
This project provides the appropriate index.d.ts
file for TypeScript users.
Example
Simple
/* @flow */ ; const task = ; // after timeoutconst refOne = task; // every interval, executeconst refTwo = task; // immediately execute on next tick (nextTick, immediate, timeout priority - first found)const refThree = taskdefer"task:three" ; // every interval and immediately (defer), executeconst refFour = task; // sequentially execute at an interval -// waits until the function requested to// finish before scheduling the next// timeout.// - NOTE: Awaits promises if returned// by the function. Which is not// the standard behavior.const refFive = task; // same as above but adds a deferred execution first// which occurs on the next tick.const refSix = task; // schedule an advanced async job with cancellationconst refSeven = task; // get the total # of tasks scheduledtasksize; // 7 // cancels each of the given ID's, if they existtask; // clear all tasks, killing the event queue and completing executiontask;
Features / Summaries
Auto Cancellation
Creating a task with the same id
as another task will cancel the previous task and schedule the next automatically.
Refs
task-handler
implements a concept of task refs
so that we can provide a unified API across all of the event types.
When using promises
, the promise will be resolved with the ref
for the task which allows capturing the result via ref.result
.
If an error is caught, the error object will include the ref as a property.
; /* Cancels the task, an alias for `task.cancel(ref.id)` */ : void /* Resolves the task with the value provided. */ : void /* Rejects the event and calls the error handling that is present on the task (if any). Generally used for `task.job` but works with any active task. */ : void /* top-level task handler the ref belongs to */ task: Task$Handler|};
For the complete types, view types.js.
Promises
When calling .promise()
on the task ref, after
and defer
return regular promises that resolve to the task ref with a result of the function passed. If no function is passed then the ref.result
will be undefined.
task ;
Interval tasks such as every
and everyNow
return async iterators
when their .promises()
function is called. This allows us to utilize the handy for await... of
feature of JS.
IMPORTANT: Notice that
every
,everySequential
, and their related siblings use.promises()
and.promise()
. If other functions call.promises()
which are not interval types they will throw an error.
{ const ref = task; for await const ref of ref console; // based on some logic... ref; console;} // Or using standard async function... { const iter = task; let done = false; let ref; while !done let ref; value: ref done = await iternext; console; // based on some logic... ref; console;} // if we want to handle errors that are not caught// in the task runner we can utilize a while loop: { const ref = task; while true try for await const result of ref console; return; catch e console; }