if-async
async conditional execution for async.js or standalone usage
Example 1: Using with Async.js Series
var async = var ifAsync = async { ... } { fs } { fs } { ... } { fs } { ... } { ... } { ... }
Example 2: Using with Async.js waterfall
var async = var ifAsync = async { } { console // prints 1 // this will cause c1 to be executed rather than c2} { console // prints 1 } { console // prints 1 } { console // prints 2 because the c1 passed 2 in the callback }
Example 3: Standalone usage
var ifAsync = var functor = { fs } { ... } { ... }
API Reference
Two main concepts that are used throughout this reference are predicate and consequent.
Predicate
in the context of ifAsync, a predicate is an async function that calls back with an error or a value. That value evaluates to true or false. e.g
{ if bar return else if foo return // same as callback(null, false) else return }
Consequent
in the context of ifAsync a consequent is an async function that is invoked as a result of an evaluation of a predicate, e.g
{ }
ifAsync(Array|Function)
Call this function with an array of functions or a single function the return value is a new function that will run all the logic and invoke a callback parameter in the end:
var functor =
When calling with an array of functions, the array is divided into pairs. The pair's first member is a predicate and the second is a consequent. If the array contains an odd number of functions then the last function is considered the default consequent (else clause)
When calling with a single function, it is considered a predicate and the user is expected to call then() at least once (an error will be thrown otherwise)
Both array style calls and fluent interface can be mixed. e.g ifAsync(f1,f2).elseIf(f3).then(f4)
which is equivalent to ifAsync(f1).then(f2).elseIf(f3).then(f4)
ifAsync.not(Function)
Same as ifAsync, only this call only accepts a predicate function and will negate its result
.then(Function)
f1 is a predicate and f2 is a consequent. Each ifAsync must include at least one then consequent
.and(Function)
connect predicates f1 and f2 with a logical AND operator
.and.not(Function)
same as and() only negate the result of the predicate
.or(Function)
connect predicates f1 and f2 with a logical OR operator
.or.not(Function)
same as or() only negate the result of the predicate
.elseIf(Function)
evaluate the first predicate (f1) if it evalutes to true then run consequent f2 otherwise evaluate f3 and if that is true, run consequent f4 elseIf() is case insensitive, you can also use elseif()
.elseIf.not(Function)
same as elseIf() only negate the result of the predicate
.else(Function)
The default consequent, it is optional and is executed if f1 predicate above evaluates to false