Skip to content
This repository has been archived by the owner on Aug 18, 2018. It is now read-only.

fbeline/microasync

Repository files navigation

microasync

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.

Installation

For nodejs projects

npm install --save microasync

To use microasync in the browser download the minified file inside the dist folder.

Documentation

thunk(fn, arguments)([callback])

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 be callback(err, response).
  • arguments - arguments for fn.
  • callback(err, response) - callback with the actual result of fn 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
    });
  });
});

each(arr, function, [callback])

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 in arr.
  • 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
});

map(arr, function, [callback])

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 in arr.
  • 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
});

filter(arr, function, [callback])

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 in arr.
  • 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
});

series(tasks, [callback])

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 a callback(err, result) it must call on completion with an error err (which can be null) and an optional result value.
  • callback(err, response) - Run once all the functions have completed. This function gets a response array containing all the result arguments passed to the functions callbacks.

Example

microasync.series([
    function(callback){
        ...
        ..
        callback(null, 'foo');
    },
    function(callback){
        ...
        ..
        callback(null, 'bar');
    }
],
function(err, response){
    // response =['foo', 'bar']
});

parallel(tasks, [callback])

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']
});

waterfall(tasks, [callback])

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 a callback(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
});

More info

  • 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.

Notes

  • microasync has nothing to do with Async project, it has a totally different implementation.

License

MIT

About

Tiny library designed to handle async problems.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published