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

async-scheduler

v1.4.4

Published

![CI](https://github.com/kremi151/async-scheduler/workflows/CI/badge.svg) ![NPM](https://img.shields.io/npm/v/async-scheduler?color=green)

Downloads

100,092

Readme

async-scheduler

CI NPM

A promise based task scheduler written in TypeScript (compatible with JavaScript ES2015).

Install

Using Yarn:

yarn add async-scheduler

Using NPM:

npm i async-scheduler

Basic usage

To get started, simply create a new Scheduler instance and enqueue some promises.

import {Scheduler} from "async-scheduler";

let scheduler = new Scheduler(2); // Maximal 2 concurrent task executions

scheduler.enqueue(() => asyncFunction())
    .then(result => console.log("Success! ", result))
    .catch(error => console.log("Error... ", error));

The invocation of Scheduler.enqueue will return a promise which will resolve the request.

Prioritization of tasks

Tasks can be allocated a priority to determine their order of execution. Tasks with higher priorities will execute before tasks with lower priorities.

import {Scheduler} from "async-scheduler";

let scheduler = new Scheduler(1);

scheduler.enqueue({
        priority: 10,
        execute: () => asyncFunction()
    }).then(/* ... */).catch(/* ... */);

scheduler.enqueue({
        priority: 20,
        execute: () => anotherAsyncFunction()
    }).then(/* ... */).catch(/* ... */);

In this example, we enqueued two tasks with priorities 10 and 20. The task with priority 20 will be executed first.

Attention: In this example, we set the amount of concurrently running tasks to one. So, we only have one task running at once. If we set this value to 2 in this example, both tasks would be running at the same time.

Another detail: Newly enqueued tasks do not affect already running ones.

Prevent tasks from running concurrently

It is possible to disallow two specific tasks from running at the same time by cancelling one of them. This can be done by using mutexes. A mutex is simply a number which can be allocated to a task (similar to priority). Two tasks "collide" with each other when their mutexes are equal (using the default configuration). An optional configuration allows two tasks to "collide" when the bitwise & operation of their mutexes gives a value different than 0.

import {Scheduler, TaskCollisionStrategy} from "async-scheduler";

let scheduler = new Scheduler(1);

scheduler.enqueue({
        priority: 10,
        mutex: 3,
        execute: () => asyncFunction(),
        onTaskCollision: (otherTask) => {
            /* Check what the other task does */
            return TaskCollisionStrategy.KEEP_THIS;
        }
    }).then(/* ... */).catch(/* ... */);

scheduler.enqueue({
        priority: 10,
        mutex: 3,
        execute: () => anotherAsyncFunction(),
        onTaskCollision: (otherTask) => {
            /* Check what the other task does */
            return TaskCollisionStrategy.KEEP_OTHER;
        }
    }).then(/* ... */).catch(/* ... */);

scheduler.enqueue({
        priority: 10,
        mutex: 4,
        execute: () => yetAnotherAsyncFunction()
    }).then(/* ... */).catch(/* ... */);

In this example, every task has the same priority. The two first tasks however have the same mutex of 3. In case of a collision like in this case, the onTaskCollision implementation of the involved tasks will be called. The first of them in the queue will get its onTaskCollision method invoked first. If this method is not provided or if it returns a non-decisive strategy, the new task to be enqueued will get its onTaskCollision method invoked. In case the first one gives a decisive strategy, the second task will not be asked. In case when both tasks do not give a decisive strategy, the second task will be rejected.

Available collision strategies:

  • TaskCollisionStrategy.DEFAULT - Apply default behavior, the same as if no implementation of onTaskCollision would have been provided
  • TaskCollisionStrategy.KEEP_THIS - Keep the current task and reject the other one
  • TaskCollisionStrategy.KEEP_OTHER - Reject the current task and keep the other one
  • TaskCollisionStrategy.KEEP_BOTH - Keep both tasks. This will only be applied if both tasks in question return this as their collision strategy.
  • TaskCollisionStrategy.RESOLVE_THIS - Remove the other task from the queue and let it resolve using this task
  • TaskCollisionStrategy.RESOLVE_OTHER - Resolve the current task using the other one

Waiting for scheduler to become idle

Sometimes it might be useful to wait for the scheduler to become idle, especially when tasks enqueue sub tasks within the same scheduler.

This can be achieved using Scheduler.waitForIdle():

import {Scheduler, TaskCollisionStrategy} from "async-scheduler";

let scheduler = new Scheduler(10);

scheduler.enqueue(async () => {
    scheduler.enqueue(async () => {
        scheduler.enqueue(async () => {
         // Enqueue as many sub tasks as you like
        });
    });
});

// Let's wait for every (sub) task to complete
scheduler.waitForIdle()
    .then(() => console.log('The scheduler is now idle!'));