1.2.9 • Published 10 years ago

execon v1.2.9

Weekly downloads
3,723
License
MIT
Repository
github
Last release
10 years ago

Execon License NPM version Dependency Status BuildStatusIMGURL

Patterns of function calls.

Install

NPM_INFO

npm i execon --save
# or
bower i execon --save

Api

var exec = require('execon');

exec

Check is parameter is function, if it's - executes it with given parameters

Was:

function(callback, p1, p2, pN) {
    if (typeof callback === 'function')
        callback(p1, p2, pN);
}

Now

function(callback, p1, p2, pN) {
    exec(callback, p1, p2, pN);
}

or just

exec.ret(callback, p1, p2, pN);

exec.if

Conditional execution one of two functions

Preconditions:

function one() {
    console.log(1);
}

function two(callback) {
    setTimeout(callback, 1000);
}

Before:

if (2 > 3)
    one();
else
    two(one);
    

After:

exec.if(2 > 3, one, two);

exec.parallel

if a you need a couple async operation do same work, and then call callback, this function for you.

Node.js example.

var fs      = require('fs'),
    Util    = require('execon');

exec.parallel([
    function(callback) {
        fs.readFile('file1', callback);
    },
    function(callback) {
        fs.readFile('file2',  callback);
    }
], function(error, data1, data2) {
    if (error)
        console.log(error)
    else
        console.log(data1, data2);
});

Vanilla js example.

var ONE_SECOND  = 1000,
    TWO_SECONDS = 2000,
    func        = function(time, str, callback) {
        setTimeout(function() {
            console.log(str);
            callback(null, str);
        }, time);
    },
    
    func1       = func.bind(null, TWO_SECONDS, 'first'),
    func2       = func.bind(null, ONE_SECOND, 'second');

exec.parallel([func1, func2], function(error, str1, str2) {
    console.log(str1, str2);
});

exec.series

executes functions one-by-one

function one(callback){
    setTimeout(function() {
        console.log(1)
        callback();
    }, 1000);
}

function two(callback) {
    console.log(2);
    callback();
}

exec.series([one, two], function(error) {
    console.log(error || 'done');
});

License

MIT

1.2.9

10 years ago

1.2.8

10 years ago

1.2.7

10 years ago

1.2.6

10 years ago

1.2.5

10 years ago

1.2.4

10 years ago

1.2.3

10 years ago

1.2.2

10 years ago

1.2.1

10 years ago

1.2.0

10 years ago

1.1.1

10 years ago

1.1.0

10 years ago

1.0.1

11 years ago

1.0.0

11 years ago