0.1.3 • Published 5 years ago

controllency v0.1.3

Weekly downloads
20
License
Apache-2.0
Repository
github
Last release
5 years ago

Controllency

Launch concurrent functions (Promises) in a controlled way.

Build Status Node Version Coverage

Demo example View demo in JSFiddle

With this module you can control how many promises are being executed at the same time when you have a large quantity of them. You can do things like:

Hey Controllency, launch all these hundreds of Promises, but execute 3 of them as maximum at once. Take the time you need. And please, signal me whenever some of them finishes.

Controllency uses a little in-memory "queue" to store the pending promises to be executed as soon as possible. You can push new promises while the previous ones are still being executed or queued to be executed. When a Promise is resolved or rejected, Controllency emit an event which you can suscribe to if you need to know the succeed result or the failing reason.

Here there is a simple schema about how Controllency works:

controllency_schema

Installation

npm install controllency --save

Usage

Example 1: basic usage

import { Controllency } from 'controllency';

let controllency = new Controllency({ maxConcurrency: 2 });

seriallency.push({ fn: hardWorkFn, params: [1]});
seriallency.push({ fn: hardWorkFn, params: [2]});
seriallency.push({ fn: hardWorkFn, params: [3]});
seriallency.push({ fn: hardWorkFn, params: [4]});
seriallency.push({ fn: hardWorkFn, params: [5]});
seriallency.push({ fn: hardWorkFn, params: [6]});

function hardWorkFn(numParam: number): Promise<any>{
    console.log(`Executing hardWorkFn. numParam:${numParam}`);
    return new Promise(resolve => {
        // ... do some async process
        setImmediate(resolve);
    });
}

// If we supose that hardWorkFn returned promise takes 1 second to be resolved, output is:
// At second 0: Executing hardWorkFn. numParam: 1
// At second 0: Executing hardWorkFn. numParam: 2
// At second 1: Executing hardWorkFn. numParam: 3
// At second 1: Executing hardWorkFn. numParam: 4
// At second 2: Executing hardWorkFn. numParam: 5
// At second 2: Executing hardWorkFn. numParam: 6
0.1.3

5 years ago

0.1.2

7 years ago

0.1.1

7 years ago

0.1.0

7 years ago

0.0.5

7 years ago

0.0.4

7 years ago

0.0.3

7 years ago

0.0.2

7 years ago

0.0.1

7 years ago