New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

task-handler

Package Overview
Dependencies
Maintainers
1
Versions
33
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

task-handler

Handle Javascript Timers in a manageable way

  • 2.1.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
24
increased by71.43%
Maintainers
1
Weekly downloads
 
Created
Source

task-handler

npm Build Status Known Vulnerabilities Coverage Status Flow Coverage license npm bundle size (minified + gzip)

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

This project provides .flow.js files for Flow to utilize. It also attempts to provide 100% test coverage.

Example

Simple

/* @flow */

import createTaskHandler from "task-handler";

const task = createTaskHandler("simple");

// after timeout
task.after("task:one", 3000, () => log("task:one execute"));

// every interval, execute
task.every("task:two", 3000, () => log("task:two execute"));

// immediately execute on next tick (nextTick, immediate, timeout priority - first found)
task.defer("task:three", () => log("task:three execute"));

// every interval and immediately (defer), execute
task.everyNow("task:four", 3000, () => log("task:four execute"));

// schedule an advanced async job with cancellation
task.job(
  "task:five",
  function TaskFiveHandler(...args) {
    // args resolves to [1, 2, 3]
    // saved context - `this` resolves to the job `ref`
    const ref = this;
    return {
      async start(ref2) {
        // called when the job starts (synchronously)
        //
        // ref is also the first argument given, it is the same as the
        // top level `this` but can be used when using arrow function
        // at the top level.
        // ref.resolve('value');
        // ref.reject('error');
        // ref.cancel();
      },
      async cancelled() {
        // called if the job is cancelled
      },
      async complete() {
        // called when the job is complete (resolved, cancelled, or errored).
      }
    };
  },
  [1, 2, 3]
);

// get the total # of tasks scheduled
task.size; // 5

// cancels each of the given ID's, if they exist
task.cancel("task:one", "task:two");

// clear all tasks, killing the event queue and completing execution
task.after("complete", 10000, () => {
  log("complete - clearing tasks");
  task.clear();
});

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
  .after("afterID", 1000, () => 1)
  .promise()
  .then(ref => {
    console.log("After 1000ms: ", ref.result); // After 1000ms: 1
  });

Interval tasks such as every and everyNow return async iterators when their .promise() function is called. This allows us to utilize the handy for await... of feature of JS.

async function intervalForAwait() {
  for await (const ref of task.every("everyID", 1000).promise()) {
    console.log("Next Tick");
    // based on some logic...
    ref.cancel();
  }
  console.log("After Cancel everyID");
}

// Or using standard async function...
async function intervalAwait() {
  const iter = task.every("everyID", 1000).promise();
  let done = false;
  let ref;
  while (!done) {
    let ref;
    ({ value: ref, done } = await iter.next());
    console.log("Next Tick");
    // based on some logic...
    ref.cancel();
  }
  console.log("After Cancel everyID");
}

Keywords

FAQs

Package last updated on 27 Nov 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc