npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

recurringtasks

v1.0.2

Published

Javascript manager to handle recurring parallel tasks

Downloads

6

Readme

Javascript Recurring Parallel Tasks

A simple javascript manager to execute repetitive parallel tasks.

Install

npm i recurringtasks

Usage

let RecurringTask = require('recurringtasks');

new RecurringTask(
  'banana',

  //can be any type of function; generators are cancelable
  function*() {
    this.count = this.count || 0;
    this.count++;

    if (this.count === 5) {
      this.goOn(false);
      throw new Error('Five bananas is too much!');
    }

    console.log('Peeling the banana...');
    yield new Promise(a => setTimeout(() => a('peeled'), 1000));

    console.log('Eating the banana...');
    yield new Promise(a =>
      //the 3rd banana will timeout
      setTimeout(() => a('eaten'), this.count === 3 ? 6000 : 2000)
    );

    console.log(
      'I ate ' + this.count + ' banana' + (this.count > 1 ? 's' : '')
    );

    return 'Result of task banana';
  },
  {
    //delay between repetitions, in seconds
    delay: 2,

    //maximum execution time, in seconds:
    timeout: 7,

    //execute only if no errors occurred
    success(r) {
      console.log(
        `Banana has been executed ${this.count} times, result is: ${r}`
      );
    },

    //always execute before the task
    before() {
      console.log('I will eat a banana now.');
    },

    //always execute after the task
    after(r) {
      console.log('Result is available inside after, too: ' + r + '\n');
    },

    //execute only if some error occurs
    error(e) {
      console.log(e);
    }
  }
).run();

If you neet to access the promises results, use an async function:

new RecurringTask(
  'blink',

  async function() {
    this.count = this.count || 0;
    this.count++;

    if (this.count === 4) {
      this.goOn(false);
      throw new Error('Stop blinking!');
    }

    let eye = await new Promise(a => 
      setTimeout(() => a(Math.random() > 0.5 ? 'left' : 'right'), 1000)
    );
    console.log('I blinked with my ' + eye + ' eye');
  },

  {
    delay: 1
  }
).run();

Danger

Promises without await are not intercepted with the manager catch:

new RecurringTask(
  'sayHello',

  async function() {
    this.count = this.count || 0;
    this.count++;

    if (this.count === 4) {
      this.goOn(false);
      throw new Error('Goodbye');
    }

    let timeout = new Promise((a, reject) => 
      setTimeout(() => reject('Your hello is too slow.'),
      3000)
    );

    //sleep for five seconds:
    await new Promise(a => setTimeout(a, 5000));

    //this will execute, even if timeout rejects.
    console.log('Hello');
  },
  {
    delay: 1
  }
).run();

The right way

new RecurringTask(
  'sayHello',

  async function() {
    this.count = this.count || 0;
    this.count++;

    if (this.count === 4) {
      this.goOn(false);
      throw new Error('Goodbye');
    }

    let timeout = new Promise((a, reject) =>
      setTimeout(
        () => reject('Your hello is too slow.'),
        3000
      )
    );

    let sleep = new Promise(a => setTimeout(() => {
      //this will execute :(
      //is easy to solve this, but if it were a big task, without access
      //to its scope, would be complicated
      console.log('Tiny nap');
      a();
    }, 5000));

    await Promise.all([timeout, sleep]);

    //this will not execute
    console.log('Hello');
  },
  {
    delay: 1,
    error(e) {
      console.log('Error: ', e);
    }
  }
).run();