Comparing version 2.3.1 to 3.3.1
31
index.js
@@ -10,3 +10,3 @@ var util = require('util') | ||
var clauses = Array.prototype.slice.call(arguments, 0) | ||
var clauses = toArray(arguments) | ||
var elseClause = elseNoop | ||
@@ -26,3 +26,3 @@ var fluentState = OK | ||
var functor = function(callback) { | ||
var functor = function() { | ||
if (fluentState !== OK) { | ||
@@ -32,6 +32,13 @@ throw new Error('missing at least one consequent, you forgot to call then() ?') | ||
var args = arguments | ||
var callback = args[args.length - 1] | ||
if (typeof callback !== 'function') { | ||
throw new Error('missing callback argument') | ||
} | ||
var predicate = clauses.shift() | ||
if (!predicate) { | ||
return elseClause(callback) | ||
return elseClause.apply(null, args) | ||
} | ||
@@ -41,11 +48,17 @@ | ||
predicate(function(err, result) { | ||
var replacedCallbackArgs = toArray(args) | ||
replacedCallbackArgs.pop() | ||
replacedCallbackArgs.push(predicateCallback) | ||
predicate.apply(null, replacedCallbackArgs) | ||
function predicateCallback(err, result) { | ||
if (err) return callback(err) | ||
if (result) { | ||
return consequent(callback) | ||
return consequent.apply(null, args) | ||
} else { | ||
functor(callback) | ||
functor.apply(null, args) | ||
} | ||
}) | ||
} | ||
} | ||
@@ -122,2 +135,6 @@ | ||
callback(null, false) | ||
} | ||
function toArray(args) { | ||
return Array.prototype.slice.call(args, 0) | ||
} |
{ | ||
"name": "if-async", | ||
"description": "", | ||
"version": "2.3.1", | ||
"version": "3.3.1", | ||
"keywords": [ | ||
@@ -6,0 +6,0 @@ "async" |
@@ -5,3 +5,3 @@ # if-async [![Build Status](https://secure.travis-ci.org/kessler/if-async.png?branch=master)](http://travis-ci.org/kessler/if-async) | ||
## Example 1: Using with Async.js | ||
## Example 1: Using with Async.js Series | ||
@@ -18,3 +18,3 @@ ```javascript | ||
bar | ||
]) | ||
], function(err) {}) | ||
@@ -31,4 +31,40 @@ function foo(callback) { ... } | ||
## Example 2: Standalone usage | ||
## Example 2: Using with Async.js waterfall | ||
```javascript | ||
var async = require('async') | ||
var ifAsync = require('if-async') | ||
async.waterfall([ | ||
foo, | ||
ifAsync(p1).then(c1).else(c2), | ||
bar | ||
], function(err) {}) | ||
function foo(callback) { | ||
callback(null, 1) | ||
} | ||
function p1(a, callback) { | ||
console.log(a) // prints 1 | ||
callback(null, true) // this will cause c1 to be executed rather than c2 | ||
} | ||
function c1(a, callback) { | ||
console.log(a) // prints 1 | ||
callback(null, 2) | ||
} | ||
function c2(a, callback) { | ||
console.log(a) // prints 1 | ||
callback(null, 3) | ||
} | ||
function bar(a, callback) { | ||
console.log(a) // prints 2 because the c1 passed 2 in the callback | ||
callback() | ||
} | ||
``` | ||
## Example 3: Standalone usage | ||
```javascript | ||
@@ -35,0 +71,0 @@ var ifAsync = require('if-async') |
16
test.js
@@ -326,2 +326,18 @@ var ifAsync = require('./index.js') | ||
it('carry arguments', function (done) { | ||
ifAsync(function(a, b, callback) { | ||
a.should.eql(1) | ||
b.should.eql(2) | ||
callback(null, true) | ||
}) | ||
.then(function(a, b, callback) { | ||
a.should.eql(1) | ||
b.should.eql(2) | ||
callback(null, 3) | ||
})(1, 2, function (err, a) { | ||
a.should.eql(3) | ||
done() | ||
}) | ||
}) | ||
function pTrue (callback) { | ||
@@ -328,0 +344,0 @@ callback(null, true) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
17182
420
151