Tiny library (~ 5kb) designed to handle famous async problems, focusing on what really matter.
There is no reason to use a library with tons of different methods when you usually will use only one or two of them.
For nodejs projects
npm install --save microasync
To use microasync in the browser download the minified file inside the dist folder.
Execute fn
passing the arguments
to it, the callback will only be triggered after the fn
complete
and you call the function that the thunk returns.
Arguments
fn
- Async function to be executed. The last param must becallback(err, response)
.arguments
- arguments forfn
.callback(err, response)
- callback with the actual result offn
response.
Examples_
var file1 = thunk(fs.open, './file1');
var file2 = thunk(fs.open, './file2');
var file3 = thunk(fs.open, './file3');
file2(function(err, response) {
if(err) return err;
// do something with file 2 response
file3(function(err, response) {
if(err) return err;
// do something with file 3 response
file1(function(err, response) {
if(err) return err;
//do someting with file 1 response
});
});
});
Iterate over array elements applying each element as a parameter of the specified function in "parallel".
Arguments
arr
- Arary to iterate over.function(el, callback)
- A function to apply to each item inarr
.callback(err, result)
- A callback which is called when all functions have finished, or an error occurs.
Examples
var filesArr = ['file1', 'file2', 'file3'];
microasync.each(filesArr, function(file, callback) {
//open the file and do something
...
}, function(err){
// if any call produced an error, err != null
});
Produces a new collection of values by mapping each element in arr
through
the function
.
Arguments
arr
- Array to iterate over.function(el, callback)
- A function to apply to each item inarr
.callback(err, result)
- A callback which is called when all functions have finished, or an error occurs.
Example
var filesArr = ['file1', 'file2', 'file3'];
microasync.map(filesArr, function(file, callback) {
fileContent = ... open file ...
callback(null, fileContent);
}, function(err, response){
//response is an array with all file contents
});
Returns a new array of all valid elements in arr
.
The response in array will be in the same order as the original.
Arguments
arr
- Array to iterate over.function(item, callback)
- A truth test to apply to each item inarr
.callback(err, result)
- A callback which is called when all functions have finished, or an error occurs.
Example
var filesArr = ['file1', 'file2', 'file3'];
microasync.filter(filesArr, function(file, callback) {
isFile = ...check if file exists ...
callback(null, isFile);
}, function(err, response){
// response is an array of the existing files
});
Run the functions in tasks
in series, each one running once the previous
function has completed.
Arguments
tasks
- An array containing functions to run, each function is passed acallback(err, result)
it must call on completion with an errorerr
(which can benull
) and an optionalresult
value.callback(err, response)
- Run once all the functions have completed. This function gets a response array containing all the result arguments passed to thefunctions
callbacks.
Example
microasync.series([
function(callback){
...
..
callback(null, 'foo');
},
function(callback){
...
..
callback(null, 'bar');
}
],
function(err, response){
// response =['foo', 'bar']
});
Run the functions in tasks
in "parallel", without waiting until the previous
function has completed.
Arguments
tasks
- An array containing functions to run.callback(err, response)
- Is executed once all the functions have completed successfully.
Example
microasync.parallel([
function(callback){
setTimeout(function(){
callback(null, 'foo');
}, 500);
},
function(callback){
setTimeout(function(){
callback(null, 'bar');
}, 250);
}
],
function(err, response){
// response = ['foo','bar']
});
Run the functions in tasks
in series, each passing their response to the next function in
the array.
Arguments
tasks
- An array of functions to run, each function is passed acallback(err, result1, result2, ...)
it must call on completion.callback(err, [response])
- An callback to run once all the functions have completed.
Example
microasync.waterfall([
function(callback) {
callback(null, 10, 20);
},
function(arg1, arg2, callback) {
var sum = arg1 + arg2; //30
callback(null, sum);
},
function(arg1, callback) {
var final = arg1 * 2; //60
callback(null, final);
}
],
function (err, response) {
// response = 60
});
- Callbacks first argument must be an error (which can be
null
) and any further arguments will be considerate as the resultant value. - If a callback returns a error the final callback will be fired immediately.
- microasync has nothing to do with Async project, it has a totally different implementation.
MIT