simple-async
Very light and simple Javascript library to execute asynchronous methods in series or in parallel. Less than 1KB for the minified version!
Getting started
This library requires NodeJS v.0.10.x in order to build the simple-async publish version, and Grunt v.0.4.1. Both of them preferable installed globally.
Install Grunt client globally:
npm install -g grunt-cli
Make a copy of the GIT repository:
git clone git://github.com/jquery/jquery.git
Install the NodeJS peer dependencies:
npm install
Setup the GIT pre-commit hooks:
grunt setup
Methods
There are three public methods:
sAsync.noConflict()
: to coexist this library with another with the same public namesAsync
.sAsync.doSeries(methods, end)
: to execute a list of methods in series, no matter they are synchronous, asynchronous or a mix of them, theend
callback will be executed at the end and return the result of the execution.sAsync.doParallel(methods, end)
: to execute a list of methods in parallel -similar to thedoSeries
method-.
Documentation:
/** * Returns the simple-async object without overwriting other one. * * @return */sAsync {}; /** * Executes asynchronously the array of methods in series and finally call the * end method with the result parameter. If there is any error, the 'end' * method will be executed before finishing the remaining unfinished methods. * * @param * the 'next' parameter should be invoqued after method is completed to * execute the next method, if it has a parameter there is an error. * @param * parameter can be undefined or other value according to a success or * unsuccess result respectively. */sAsync {}; /** * Executes asynchronously the array of methods in parallel and finally call * the end method with the result parameter. If there is any error, the 'end' * method will be executed before finishing the remaining unfinished methods. * * @param * the 'done' parameter should be invoqued after method is completed to * execute the callback, if it has a parameter there is an error. * @param * parameter can be undefined or other value according to a success or * unsuccess result respectively. */sAsync {};
Usage
Next the explanation about how to use simple-async in the browser and in the server.
In the browser
After cloning the GIT repository it is necessary to create the library for the browser. This Grunt task creates the minified version and the development one with the header license:
grunt build
The generated files are located in the publish
folder:
- Development:
publish/s-async.js
- Minified:
publish/s-async.min.js
Use:
In the server
Simply download the module simple-async via npm:
npm install simple-async
Use:
var sAsync = ; sAsync;sAsync;
Examples
- Executing asynchronous methods in series without error:
sAsync; // Output:// 'f1 finished'// 'f2 finished'// 'finished: ok'
- Executing synchronous and asynchronous methods in series with error:
sAsync; // Output (f3 is never executed due to the f2 error):// 'f1 finished'// 'f2 finished with error'// 'finished: error executing f2'
- Executing asynchronous methods in parallel without error:
sAsync; // Output:// 'f1 is the third to finish'// 'f2 is the first to finish'// 'f3 is the second to finish'// 'f2 finished'// 'f3 finished'// 'f1 finished'// 'finished: ok'
- Executing synchronous and asynchronous methods in parallel with error:
sAsync; // Output:// 'f1 is the third to finish'// 'f2 is the second to finish'// 'f3 is the first to finish'// 'f3 finished'// 'f2 finished with error'// 'finished: error executing f2'// 'f1 finished'
Running development tools
- Unit tests using mocha framework:
grunt test
- Javascript linter tools:
- jshint:
grunt jshint
- Google Closure Linter: gjslint
grunt gjslint
Contributing
In order to mantain this library, anybody can contribute to enhance it, to fix bugs and much more. Take care to:
- Backward compatibility.
- Coding style -use the Grunt tasks: jshint and gjslint-.
- Execute and maintain the unit-tests.
Release History
- v0.0.1: initial version.