AsyncQueue represents a queue of task items that are processed
asynchronously. Items can be added and cancelled before they are
processed by the queue.
The AsyncQueue constructor expects to receive a task processing
function as an argument. The function MUST call task#done with no arguments
when it finishes a task. If an error occurs during the processing, the
done() function MUST be called on the task object with an error message.
| Name | Type | Description |
|---|---|---|
mode |
H.util.AsyncQueue.Mode |
The processing mode for the given queue |
processFn |
function(H.util.AsyncQueue.Task, *) |
The function implementing the actual processing of the task item (it receives the the task item and the task data as arguments). |
Example
//---------------------------------
// a simple example
//---------------------------------
var additionQueue = new H.util.AsyncQueue(3, function(task, data) {
// process data
var operandA = data.operandA,
operandB = data.operandB,
result = operandA + operandB;
// call task.done
task.done(result);
});
// push tasks with data to process
additionQueue.push({
operandA: 1,
operandB: 1
}, function(task, result, opt_error) { console.log(result); });
//---------------------------------
// an example with an error
//---------------------------------
var divisionQueue = new H.util.AsyncQueue(3, function(task, data) {
// process data
var operandA = data.operandA,
operandB = data.operandB;
if(operandB === 0) {
// call task.done with an error message
task.done(null, 'Division by zero!');
}
else {
result = operandA / operandB;
// call task.done
task.done(result);
}
});
// push tasks with data to process
divisionQueue.push({
operandA: 1,
operandB: 0
}, function(task, result, opt_error) {
if(opt_error) console.log("Oh noes: ", opt_error);
else console.log("Yay: ", result);
});
Classes
Methods
-
push (data, onDone)H.util.AsyncQueue.Task
-
This method pushes a task item to the queue for processing.
Name Type Description data* The task data to be passed to the process function
onDonefunction(H.util.AsyncQueue.Task, *, ?) A function to be called once the task has been processed (an optional error string can be provided for use in the even of an error or if the task is cancelled).
Returns:
Type Description H.util.AsyncQueue.Task An object representing the new task pushed to the queue
Interface Definitions
-
This interface defines a processing mode object which is used by an asynchronous queue to determine how many items to process in one cycle.