async
Advanced tools
Comparing version 0.2.3 to 0.2.4
@@ -918,2 +918,20 @@ /*global setImmediate: false, setTimeout: false, console: false */ | ||
async.compose = function (/* functions... */) { | ||
var fns = Array.prototype.reverse.call(arguments); | ||
return function () { | ||
var that = this; | ||
var args = Array.prototype.slice.call(arguments); | ||
var callback = args.pop(); | ||
async.reduce(fns, args, function (newargs, fn, cb) { | ||
fn.apply(that, newargs.concat([function () { | ||
var err = arguments[0]; | ||
var nextargs = Array.prototype.slice.call(arguments, 1); | ||
cb(err, nextargs); | ||
}])) | ||
}, | ||
function (err, results) { | ||
callback.apply(that, [err].concat(results)); | ||
}); | ||
}; | ||
}; | ||
@@ -920,0 +938,0 @@ // AMD / RequireJS |
@@ -6,3 +6,3 @@ { | ||
"author": "Caolan McMahon", | ||
"version": "0.2.3", | ||
"version": "0.2.4", | ||
"repository" : { | ||
@@ -9,0 +9,0 @@ "type" : "git", |
@@ -95,2 +95,3 @@ # Async.js | ||
* [waterfall](#waterfall) | ||
* [compose](#compose) | ||
* [queue](#queue) | ||
@@ -163,10 +164,13 @@ * [cargo](#cargo) | ||
The same as forEach only the iterator is applied to batches of items in the | ||
array, in series. The next batch of iterators is only called once the current | ||
one has completed processing. | ||
The same as forEach only no more than "limit" iterators will be simultaneously | ||
running at any time. | ||
Note that the items are not processed in batches, so there is no guarantee that | ||
the first "limit" iterator functions will complete before any others are | ||
started. | ||
__Arguments__ | ||
* arr - An array to iterate over. | ||
* limit - How many items should be in each batch. | ||
* limit - The maximum number of iterators to run at any time. | ||
* iterator(item, callback) - A function to apply to each item in the array. | ||
@@ -239,10 +243,13 @@ The iterator is passed a callback which must be called once it has completed. | ||
The same as map only the iterator is applied to batches of items in the | ||
array, in series. The next batch of iterators is only called once the current | ||
one has completed processing. | ||
The same as map only no more than "limit" iterators will be simultaneously | ||
running at any time. | ||
Note that the items are not processed in batches, so there is no guarantee that | ||
the first "limit" iterator functions will complete before any others are | ||
started. | ||
__Arguments__ | ||
* arr - An array to iterate over. | ||
* limit - How many items should be in each batch. | ||
* limit - The maximum number of iterators to run at any time. | ||
* iterator(item, callback) - A function to apply to each item in the array. | ||
@@ -676,2 +683,5 @@ The iterator is passed a callback which must be called once it has completed. | ||
Note that the tasks are not executed in batches, so there is no guarantee that | ||
the first "limit" tasks will complete before any others are started. | ||
__Arguments__ | ||
@@ -785,3 +795,41 @@ | ||
--------------------------------------- | ||
<a name="compose" /> | ||
### compose(fn1, fn2...) | ||
Creates a function which is a composition of the passed asynchronous | ||
functions. Each function consumes the return value of the function that | ||
follows. Composing functions f(), g() and h() would produce the result of | ||
f(g(h())), only this version uses callbacks to obtain the return values. | ||
Each function is executed with the `this` binding of the composed function. | ||
__Arguments__ | ||
* functions... - the asynchronous functions to compose | ||
__Example__ | ||
```js | ||
function add1(n, callback) { | ||
setTimeout(function () [ | ||
callback(null, n + 1); | ||
}, 10); | ||
} | ||
function mul3(n, callback) { | ||
setTimeout(function () [ | ||
callback(null, n * 3); | ||
}, 10); | ||
} | ||
var add1mul3 = async.compose(mul3, add1); | ||
add1mul3(4, function (err, result) { | ||
// result now equals 15 | ||
}); | ||
``` | ||
--------------------------------------- | ||
<a name="queue" /> | ||
@@ -788,0 +836,0 @@ ### queue(worker, concurrency) |
118664
5
883
1294