Skip to content

Launch concurrent functions (Promises) in a controlled way

License

Notifications You must be signed in to change notification settings

davloperez/controllency

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

36 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

Launch concurrent functions (Promises) in a controlled way

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published