whilst

splunkjs.Async.whilst

An asynchronous while loop

Syntax

root.whilst = function(condition, body, callback)

Parameters

Name Type Description
condition Function

A function which returns a boolean depending on whether the condition has been met.

body Function

A function which executes the body of the loop: (done)

callback Function

A function to be executed when the loop is complete: (err)

Examples

 var i = 0;
 Async.whilst(
     function() { return i++ < 3; },
     function(done) {
         Async.sleep(0, function() { done(); });
     },
     function(err) {
         console.log(i) // == 3;
     }
 );

parallel

splunkjs.Async.parallel

Execute multiple functions in parallel.

Async.parallel will execute multiple tasks (functions) in parallel, and only call the callback if one of them fails, or when all are complete.

Each task takes a single parameter, which is a callback to be invoked when the task is complete.

The callback will be invoked with the combined results (in order) of all the tasks.

Note that order of execution is not guaranteed, even though order of results is.

Syntax

root.parallel = function(tasks, callback)

Parameters

Name Type Description
tasks Function

An array of functions: (done)

callback Function

A function to be executed when all tasks are done or an error occurred: (err, ...)

Examples

 Async.parallel([
     function(done) {
         done(null, 1);
     },
     function(done) {
         done(null, 2, 3);
     }],
     function(err, one, two) {
         console.log(err); // == null
         console.log(one); // == 1
         console.log(two); // == [1,2]
     }
 );

series

splunkjs.Async.series

Execute multiple functions in series.

Async.series will execute multiple tasks (functions) in series, and only call the callback if one of them fails, or when all are complete.

Each task takes a single parameter, which is a callback to be invoked when the task is complete.

The callback will be invoked with the combined results (in order) of all the tasks.

Syntax

root.series = function(tasks, callback)

Parameters

Name Type Description
tasks Function

An array of functions: (done)

callback Function

A function to be executed when all tasks are done or an error occurred: (err, ...)

Examples

 var keeper = 0;
 Async.series([
     function(done) {
         Async.sleep(10, function() {
             console.log(keeper++); // == 0
             done(null, 1);
         });
     },
     function(done) {
         console.log(keeper++); // == 1
         done(null, 2, 3);
     }],
     function(err, one, two) {
         console.log(err); // == null
         console.log(one); // == 1
         console.log(two); // == [1,2]
     }
 );

parallelMap

splunkjs.Async.parallelMap

Map an asynchronous function over an array of values, in parallel.

Async.parallelMap will execute a function over each element in an array in parallel, and only call the callback when all operations are done, or when there is an error.

The callback will be invoked with the resulting array.

Syntax

root.parallelMap = function(vals, fn, callback)

Parameters

Name Type Description
vals Array

An array of the values to map over.

fn Function

A (possibly asycnhronous) function to apply to each element: (done)

callback Function

A function to be executed when all tasks are done or an error occurred: (err, mappedVals)

Examples

 Async.parallelMap(
     [1, 2, 3],
     function(val, idx, done) { 
         if (val === 2) {
             Async.sleep(100, function() { done(null, val+1); });   
         }
         else {
             done(null, val + 1);
         }
     },
     function(err, vals) {
         console.log(vals); // == [2,3,4]
     }
 );

seriesMap

splunkjs.Async.seriesMap

Map an asynchronous function over an array of values, in series.

Async.seriesMap will execute a function over each element in an array in series, and only call the callback when all operations are done, or when there is an error.

The callback will be invoked with the resulting array.

Syntax

root.seriesMap = function(vals, fn, callback)

Parameters

Name Type Description
vals Array

An array of the values to map over.

fn Function

A (possibly asycnhronous) function to apply to each element: (done)

callback Function

A function to be executed when all tasks are done or an error occurred: (err, mappedVals)

Examples

 var keeper = 1;
 Async.seriesMap(
     [1, 2, 3],
     function(val, idx, done) { 
         console.log(keeper++); // == 1, then 2, then 3
         done(null, val + 1);
     },
     function(err, vals) {
         console.log(vals); // == [2,3,4];
     }
 );

parallelEach

splunkjs.Async.parallelEach

Apply an asynchronous function over an array of values, in parallel.

Async.parallelEach will execute a function over each element in an array in parallel, and only call the callback when all operations are done, or when there is an error.

The callback will be invoked with nothing except a possible error parameter

Syntax

root.parallelEach = function(vals, fn, callback)

Parameters

Name Type Description
vals Array

An array of the values to apply over.

fn Function

A (possibly asycnhronous) function to apply to each element: (done)

callback Function

A function to be executed when all tasks are done or an error occurred: (err)

Examples

 var total = 0;
 Async.parallelEach(
     [1, 2, 3],
     function(val, idx, done) { 
         var go = function() {
             total += val;
             done();
         };
         if (idx === 1) {
             Async.sleep(100, go);    
         }
         else {
             go();
         }
     },
     function(err) {
         console.log(total); // == 6
     }
 );

seriesEach

splunkjs.Async.seriesEach

Apply an asynchronous function over an array of values, in series.

Async.seriesEach will execute a function over each element in an array in series, and only call the callback when all operations are done, or when there is an error.

The callback will be invoked with nothing except a possible error parameter

Syntax

root.seriesEach = function(vals, fn, callback)

Parameters

Name Type Description
vals Array

An array of the values to apply over.

fn Function

A (possibly asycnhronous) function to apply to each element: (done)

callback Function

A function to be executed when all tasks are done or an error occurred: (err)

Examples

 var results = [1, 3, 6];
 var total = 0;
 Async.seriesEach(
     [1, 2, 3],
     function(val, idx, done) { 
         total += val;
         console.log(total === results[idx]); //== true
         done();
     },
     function(err) {
         console.log(total); //== 6
     }
 );

chain

splunkjs.Async.chain

Chain asynchronous tasks.

Async.chain will chain asynchronous together by executing a task, and passing the results to the next task as arguments. If an error occurs at any point, or when the chain completes, the callback will be executed

Each task takes 1-N parameters, where the amount is dependent on the previous task in the chain. The last parameter will always be a function to invoke when the task is complete.

The callback will be invoked with the result of the final task.

Note that err arguments are not passed to individual tasks - they are propagated to the final callback.

Syntax

root.chain = function(tasks, callback)

Parameters

Name Type Description
tasks Function

An array of functions: (done)

callback Function

A function to be executed when the chain is done or an error occurred: (err, ...)

Examples

Async.chain(
    function(callback) { 
        callback(null, 1, 2);
    },
    function(val1, val2, callback) {
        callback(null, val1 + 1);
    },
    function(val1, callback) {
        callback(null, val1 + 1, 5);
    },
    function(err, val1, val2) {
        console.log(val1); //== 3
        console.log(val2); //== 5
    }
);

sleep

splunkjs.Async.sleep

Execute a function after a certain delay.

Async.sleep will execute the given function after the specified timeout period. This function mostly exists to make setTimeout adhere to Node.js style function signatures.

Syntax

root.sleep = function(timeout, callback)

Parameters

Name Type Description
timeout Number

The specified timeout in milliseconds.

callback Function

A function to be executed when the timeout occurs.

Examples

Async.sleep(1000, function() { console.log("TIMEOUT");});

augment

splunkjs.Async.augment

Augment a callback with extra parameters

Async.augment will cause a callback to be invoked with the extra specified parameters.

Note that the augmented parameters are appended to the end of the parameter list.

Syntax

root.augment = function(callback)

Parameters

Name Type Description
callback Function

The callback to augment.

rest Anything...

Variable number of arguments to augment the callback with.

Examples

 var callback = function(a, b) {
     console.log(a); //== 1
     console.log(b); //== 2
 };
 var augmented = Async.augment(callback, 2);
 augmented(1);