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 optionsObject Parameters
Properties
Name Type Argument Description namestring the name of the iterator
processingDataArray.<*> an array that has to be processed. For each element of array the options.processingFunction will be invited to process it
processingFunctionprocessingFunctionCallback function that is called for processing data
thisObjObject <optional>
the object/context in which processing function will be called
processingArgumentsArray.<*> <optional>
arguments that will be passed to the options.processingFunction in addition to element to process and few other maintance parameters
limitConcurrentlyNumnumber(integer) the number of concurrently processing array elements (calls to the options.processingFunction)
limitPerSecondnumber(integer) the maximum number array elements to process per second
debugboolean defines if debugging messages should be shown during mapBandwidth processing
iteratorObject <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
indexnumber(index) index of the processing element in the array
processingArgs* <optional>
<repeatable>
additional arguments passed with options.processingArguments
callbackprocessingFunctionFinishedCallback 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 indexnumber(index) index of the processing element in the array that has been processed @see
processingFunctionCallback