qsync
v0.0.8
Published
[Under development]
Downloads
4
Readme
qsync
Quick sync is a two API solution to synchronize asynchronous function calls. It helps to implement common control flow scenarios easily.
Installation
This module is originally written for nodejs. Though it's planned to port into other areas in future.
$ npm install qsync
##Quick Example
// Synchronize with states
qsync.serial([10, 20, 30], {
idle: function (input, callback) { ... },
active: function (result, callback) { ... },
close: function (result, callback) { ... }
},
function (error, results) {
});
// Classic synchronization
qsync.serial([10, 20, 30], [
function (input, callback) { ... },
function (result, callback) { ... }
], callback);
// Process inputs in parallel
qsync.parallel([10, 20, 30], [
function(input, callback){ ... },
function(result, callback){ ... }
],
function (error, results) {
});
##Contents
Arguments
input
- Optional- no input If no input is given, all
tasks
will be run only once. Alsoinput
argument of first task will benull
. In parallel API, alltasks
will be running concurrently.
qsync.serial([
function (input, callback) {...}
]);
- single input Given data will be passed as the
input
argument of first task. It can be a number, string or a json object. Alltasks
will be run only once. In parallel API, alltasks
will be running concurrently.
qsync.serial(15, [
function (input, callback) {...}
]);
- array input In case of an array input, each array element will be passed as the
input
argument of first task. If array hasn
number of elements,tasks
will be runn
times each corresponds to a respective data in array. In both API, a group oftasks
are created to process each input. The groups can run either sequentially (in serial API) or concurrently (in parallel API). Though thetasks
inside a group will be running in serial by default in both the APIs. In parallel API, each array element will be processed in parallel. Note that the tasks inside a group are still running serially and this default behavior can be modified by overriding flow control middleware. This default behavior is different inno input
and insingle input
scenario. In below example, a group of task1 and task2 for processing input 10 will be created and running in serial. another group of task1 and task2 for processing input 15 will also be created and running in serial and a third task group for processing 20 will also be running similarly. But in parallel API, all these groups will be running in parallel so that all inputs will be processed in parallel. But inside any of the three group, task1 and task2 will be running in serial.
qsync.serial([10, 15, 20], {
task1: function (input, callback) {...},
task2: function (result, callback) {...}
});
- variadic input This input is same as
single input
. All the inputs will be passed as variadic arguments of first task. In below example, 3, 5 and 11 will be passed to first task as its 1st, 2nd and 3rd argument respectively.callback
will be its 4th argument.
qsync.serial(3, 5, 11, [
function (input, callback) {...}
]);
tasks
- single task Only one function will be given as a task with following syntax.
function (input1, input2, ..., callback) {}
input1
, input2
, etc are the input
argument provided to the API or the input passed via callback
of previously called task.
qsync.serial([10, 15, 20], function (input, callback) {...});
- json tasks Instead of giving a single task, it's possible to give a number of labeled task so that the
input
can be processed as a state machine.
{
label1: function (input1, input2, ..., callback) {},
label2: function (input1, input2, ..., callback) {},
:
labelN: function (input1, input2, ..., callback) {}
}
In above syntax, all functions should have the same syntax as explained in single task.
qsync.serial([10, 15, 20], {
idle: function (input, callback) {...},
active: function (result, callback) {...},
close: function (result, callback) { ... }
});
- array of tasks An array of functions can also be given as
tasks
if labels are not significant.
[
function (input1, input2, ..., callback) {},
function (input1, input2, ..., callback) {},
:
function (input1, input2, ..., callback) {}
]
In above syntax, all functions should have the same syntax as explained in single task.
qsync.serial([10, 15, 20], [
function (input, callback) {...},
function (result, callback) {...}
]);
callback(error, result)
- An optional callback which is called when all inputs are processed or an error occurs. By default theresult
argument will be the result returned by lasttask
. However it's possible to change this behavior by overriding middleware as explained in sectionStorage
. If lasttask
didn't pass a result, onlyerror
argument will be passed to this callback.
Copyright (c) 2015 Fazil Vadakkumpadath
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.