Coder Social home page Coder Social logo

agustinsrg / async-tools Goto Github PK

View Code? Open in Web Editor NEW
1.0 3.0 0.0 736 KB

Collection of tools to work with async funcions in javascript.

Home Page: https://agustinsrg.github.io/async-tools/

License: MIT License

JavaScript 7.70% TypeScript 92.30%
promise async javascript await queue semaphore interval

async-tools's Introduction

Async tools

npm version

Collection of tools to work with async funcions in javascript.

Installation

If you are using a npm managed project use:

npm install @asanrom/async-tools

If you are using it in the browser, download the minified file from the Releases section and import it to your html:

<script type="text/javascript" src="/path/to/async-tools.js"></script>

The browser library exports all artifacts to the window global: AsyncTools

Async Interval

Interval that waits for the async function to end before runnng it again. Prevent multiple simultaneous executions.

Example use case: Async periodic task

Usage:

import { AsyncInterval } from "@asanrom/async-tools";

const interval = new AsyncInterval(async function () {
    await doSomethingAsync();
}, 1000 /* Milliseconds */);

interval.on("error", error => {
    // If the promise is rejected it will emit
    // and error event. If you want the interval to continue
    // when this happens, you have to assign an error handler
    console.error(error);
});

interval.start(); // Start the interval

interval.stop(); // Stops / Clears the interval

Async Queue

Queue with an async item handler.

  • Items are handled in order (FIFO)
  • If the handler is an async function, it waits for it to finish before dispatching the next item

Usage:

import { AsyncQueue } from "@asanrom/async-tools";

const queue = new AsyncQueue(
    MAX_SIZE, // Max size of the queue or 0 for unlimited size
    async function (item) { // Item handler
        await doSomethingAsync(item)
    }
);

queue.on("error", error => {
    // If the promise is rejected it will emit
    // and error event. If you want the queue to continue
    // when this happens, you have to assign an error handler
    console.error(error);
});

const items = [1, 2, 3, 4];
items.forEach(item => {
    // Use push(item) to push items to the queue
    // They will be dispatched automatically
    // Push will return false if the item was dropped
    queue.push(item);
});

// We can check the size of the queue (number of items in it)
queue.getCurrentSize();

// Also we can check if it's full
queue.isFull();

// If we want to release the resources of the queue
// we can call destroy()
// It returns a promise that waits if there is an item
// in the mid of being handled
await queue.destroy();

Async Semaphore

Semaphore to create critical sections on async functions.

Usage:

import { AsyncSemaphore } from "@asanrom/async-tools";


const sem = new AsyncSemaphore(); // Without params, initial instances is 1 (Mutex)
const sem3Instances = new AsyncSemaphore(3); // 3 initial instances

// Acquire instances, if it can't acquire
// the promise will resolve when the instances are available
// it will reject if the semaphore is destroyed
await sem.acquire();

// Release instances and resolve the promises
sem.release();

// Rejects all promises waiting to acquire the semaphore
// After destroyed, it cannot be used anymore
sem.destroy();

Documentation

async-tools's People

Contributors

agustinsrg avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.