Info
This module provides different Promise related (implemented with) patterns and sollutions It contains semaphore implementation for syncing consumers of resources (like simultaneous writing in files, etc), and concurrent itterators that are limited by number of parallel execution of iterators (if we want to limit number of parallel acceses to webservice, etc).
Dependencies
This module requires q npm module (please check also the @see q github)
Requires
- module:q
Methods
-
<inner> mapBandwidth - Bandwidth limited iteration(options, iterator)
-
Parameters:
Name Type Argument Default Description options
Object Parameters
Properties
Name Type Argument Description name
string the name of the iterator
processingData
Array.<*> an array that has to be processed. For each element of array the options.processingFunction will be invited to process it
processingFunction
processingFunctionCallback function that is called for processing data
thisObj
Object <optional>
the object/context in which processing function will be called
processingArguments
Array.<*> <optional>
arguments that will be passed to the options.processingFunction in addition to element to process and few other maintance parameters
limitConcurrentlyNum
number(integer) the number of concurrently processing array elements (calls to the options.processingFunction)
limitPerSecond
number(integer) the maximum number array elements to process per second
debug
boolean defines if debugging messages should be shown during mapBandwidth processing
iterator
Object <optional>
{} iterator that keeps information of the iteration status and options
Returns:
iterator that keeps information of the iteration status and options
- Type
- Object
Example
var Q = require('q'); var QPP = require('./..'); var iterator = {}; var sum = 0; var processingFunction = function(data, index){ console.log("[processingFunction:starting] data: %s, iterator.processingCurrentNo: %d", data, iterator.processingCurrentNo); // test for the limit of concurrently running functions var defered = Q.defer(); setTimeout(function(){ sum += data; console.log("[processingFunction:finishing] data: %s, iterator.processingCurrentNo: %d", data, iterator.processingCurrentNo); defered.resolve(); }, parseInt(Math.random()*100)+1); return defered.promise; }; var options = {}; options.processingData = [0, 1, 2, 3, 4, 5]; options.limitConcurrentlyNum = 3; options.processingFunction = processingFunction; iterator = QPP.mapBandwidth(options, iterator); var promise = iterator.$promise; promise.then(function(processedNo){ console.log("Done: processed: %d, sum: %d", processedNo, sum); });
Type Definitions
-
processingFunctionCallback(dataElement, index, processingArgs, callback)
-
This is a type (a signature) of a processing function (callback) that is called for every element to be processed in the case of itterators (mapBandwidth, etc)
Parameters:
Name Type Argument Description dataElement
* data element to be processed
index
number(index) index of the processing element in the array
processingArgs
* <optional>
<repeatable>
additional arguments passed with options.processingArguments
callback
processingFunctionFinishedCallback callback from the processing function back, when the processing is finished
Returns:
promise that will get realized after function is available
- Type
- Promise
-
processingFunctionFinishedCallback(index)
-
This is a type (a signature) of a processing function callback, called back from the processing function
processingFunctionCallback
. Processing function calls the iterator (mapBandwidth etc) when it finishes processing the processing element. Note: the more preferred way is that function returns a promise and communicate with iterator through the promise instead through callbackParameters:
Name Type Description index
number(index) index of the processing element in the array that has been processed @see
processingFunctionCallback