Comparing version 0.2.6 to 0.2.7
114
lib/async.js
@@ -78,7 +78,7 @@ /*global setImmediate: false, setTimeout: false, console: false */ | ||
if (typeof setImmediate === 'function') { | ||
async.nextTick = function (fn) { | ||
setImmediate(fn); | ||
}; | ||
async.setImmediate = setImmediate; | ||
async.nextTick = setImmediate; | ||
} | ||
else { | ||
async.setImmediate = async.nextTick; | ||
async.nextTick = function (fn) { | ||
@@ -91,2 +91,8 @@ setTimeout(fn, 0); | ||
async.nextTick = process.nextTick; | ||
if (typeof setImmediate !== 'undefined') { | ||
async.setImmediate = setImmediate; | ||
} | ||
else { | ||
async.setImmediate = async.nextTick; | ||
} | ||
} | ||
@@ -124,3 +130,2 @@ | ||
var iterate = function () { | ||
var sync = true; | ||
iterator(arr[completed], function (err) { | ||
@@ -137,12 +142,6 @@ if (err) { | ||
else { | ||
if (sync) { | ||
async.nextTick(iterate); | ||
} | ||
else { | ||
iterate(); | ||
} | ||
iterate(); | ||
} | ||
} | ||
}); | ||
sync = false; | ||
}; | ||
@@ -448,3 +447,3 @@ iterate(); | ||
results[k] = args; | ||
async.nextTick(taskComplete); | ||
async.setImmediate(taskComplete); | ||
} | ||
@@ -475,2 +474,6 @@ }; | ||
callback = callback || function () {}; | ||
if (tasks.constructor !== Array) { | ||
var err = new Error('First argument to waterfall must be an array of functions'); | ||
return callback(err); | ||
} | ||
if (!tasks.length) { | ||
@@ -494,3 +497,3 @@ return callback(); | ||
} | ||
async.nextTick(function () { | ||
async.setImmediate(function () { | ||
iterator.apply(null, args); | ||
@@ -617,3 +620,2 @@ }); | ||
if (test()) { | ||
var sync = true; | ||
iterator(function (err) { | ||
@@ -623,12 +625,4 @@ if (err) { | ||
} | ||
if (sync) { | ||
async.nextTick(function () { | ||
async.whilst(test, iterator, callback); | ||
}); | ||
} | ||
else { | ||
async.whilst(test, iterator, callback); | ||
} | ||
async.whilst(test, iterator, callback); | ||
}); | ||
sync = false; | ||
} | ||
@@ -641,3 +635,2 @@ else { | ||
async.doWhilst = function (iterator, test, callback) { | ||
var sync = true; | ||
iterator(function (err) { | ||
@@ -648,10 +641,3 @@ if (err) { | ||
if (test()) { | ||
if (sync) { | ||
async.nextTick(function () { | ||
async.doWhilst(iterator, test, callback); | ||
}); | ||
} | ||
else { | ||
async.doWhilst(iterator, test, callback); | ||
} | ||
async.doWhilst(iterator, test, callback); | ||
} | ||
@@ -662,3 +648,2 @@ else { | ||
}); | ||
sync = false; | ||
}; | ||
@@ -668,3 +653,2 @@ | ||
if (!test()) { | ||
var sync = true; | ||
iterator(function (err) { | ||
@@ -674,12 +658,4 @@ if (err) { | ||
} | ||
if (sync) { | ||
async.nextTick(function () { | ||
async.until(test, iterator, callback); | ||
}); | ||
} | ||
else { | ||
async.until(test, iterator, callback); | ||
} | ||
async.until(test, iterator, callback); | ||
}); | ||
sync = false; | ||
} | ||
@@ -692,3 +668,2 @@ else { | ||
async.doUntil = function (iterator, test, callback) { | ||
var sync = true; | ||
iterator(function (err) { | ||
@@ -699,10 +674,3 @@ if (err) { | ||
if (!test()) { | ||
if (sync) { | ||
async.nextTick(function () { | ||
async.doUntil(iterator, test, callback); | ||
}); | ||
} | ||
else { | ||
async.doUntil(iterator, test, callback); | ||
} | ||
async.doUntil(iterator, test, callback); | ||
} | ||
@@ -713,3 +681,2 @@ else { | ||
}); | ||
sync = false; | ||
}; | ||
@@ -740,3 +707,3 @@ | ||
} | ||
async.nextTick(q.process); | ||
async.setImmediate(q.process); | ||
}); | ||
@@ -765,3 +732,2 @@ } | ||
workers += 1; | ||
var sync = true; | ||
var next = function () { | ||
@@ -777,15 +743,4 @@ workers -= 1; | ||
}; | ||
var cb = only_once(function () { | ||
var cbArgs = arguments; | ||
if (sync) { | ||
async.nextTick(function () { | ||
next.apply(null, cbArgs); | ||
}); | ||
} else { | ||
next.apply(null, arguments); | ||
} | ||
}); | ||
var cb = only_once(next); | ||
worker(task.data, cb); | ||
sync = false; | ||
} | ||
@@ -826,3 +781,3 @@ }, | ||
}); | ||
async.nextTick(cargo.process); | ||
async.setImmediate(cargo.process); | ||
}, | ||
@@ -969,3 +924,3 @@ process: function process() { | ||
async.applyEach = function (fns /*args...*/) { | ||
var _applyEach = function (eachfn, fns /*args...*/) { | ||
var go = function () { | ||
@@ -975,3 +930,3 @@ var that = this; | ||
var callback = args.pop(); | ||
return async.each(fns, function (fn, cb) { | ||
return eachfn(fns, function (fn, cb) { | ||
fn.apply(that, args.concat([cb])); | ||
@@ -981,4 +936,4 @@ }, | ||
}; | ||
if (arguments.length > 1) { | ||
var args = Array.prototype.slice.call(arguments, 1); | ||
if (arguments.length > 2) { | ||
var args = Array.prototype.slice.call(arguments, 2); | ||
return go.apply(this, args); | ||
@@ -990,3 +945,18 @@ } | ||
}; | ||
async.applyEach = doParallel(_applyEach); | ||
async.applyEachSeries = doSeries(_applyEach); | ||
async.forever = function (fn, callback) { | ||
function next(err) { | ||
if (err) { | ||
if (callback) { | ||
return callback(err); | ||
} | ||
throw err; | ||
} | ||
fn(next); | ||
} | ||
next(); | ||
}; | ||
// AMD / RequireJS | ||
@@ -993,0 +963,0 @@ if (typeof define !== 'undefined' && define.amd) { |
@@ -6,3 +6,3 @@ { | ||
"author": "Caolan McMahon", | ||
"version": "0.2.6", | ||
"version": "0.2.7", | ||
"repository" : { | ||
@@ -9,0 +9,0 @@ "type" : "git", |
@@ -41,7 +41,42 @@ # Async.js | ||
## Common Pitfalls | ||
### Binding a context to an iterator | ||
This section is really about bind, not about async. If you are wondering how to | ||
make async execute your iterators in a given context, or are confused as to why | ||
a method of another library isn't working as an iterator, study this example: | ||
```js | ||
// Here is a simple object with an (unnecessarily roundabout) squaring method | ||
var AsyncSquaringLibrary = { | ||
squareExponent: 2, | ||
square: function(number, callback){ | ||
var result = Math.pow(number, this.squareExponent); | ||
setTimeout(function(){ | ||
callback(null, result); | ||
}, 200); | ||
} | ||
}; | ||
async.map([1, 2, 3], AsyncSquaringLibrary.square, function(err, result){ | ||
// result is [NaN, NaN, NaN] | ||
// This fails because the `this.squareExponent` expression in the square | ||
// function is not evaluated in the context of AsyncSquaringLibrary, and is | ||
// therefore undefined. | ||
}); | ||
async.map([1, 2, 3], AsyncSquaringLibrary.square.bind(AsyncSquaringLibrary), function(err, result){ | ||
// result is [1, 4, 9] | ||
// With the help of bind we can attach a context to the iterator before | ||
// passing it to async. Now the square function will be executed in its | ||
// 'home' AsyncSquaringLibrary context and the value of `this.squareExponent` | ||
// will be as expected. | ||
}); | ||
``` | ||
## Download | ||
Releases are available for download from | ||
[GitHub](http://github.com/caolan/async/downloads). | ||
The source is available for download from | ||
[GitHub](http://github.com/caolan/async). | ||
Alternatively, you can install using Node Package Manager (npm): | ||
@@ -51,11 +86,7 @@ | ||
__Development:__ [async.js](https://github.com/caolan/async/raw/master/lib/async.js) - 29.6kb Uncompressed | ||
__Development:__ [async.js](https://github.com/caolan/async/raw/master/lib/async.js) - 17.5kb Uncompressed | ||
__Production:__ [async.min.js](https://github.com/caolan/async/raw/master/dist/async.min.js) - 1.7kb Packed and Gzipped | ||
## In the Browser | ||
So far its been tested in IE6, IE7, IE8, FF3.6 and Chrome 5. Usage: | ||
So far it's been tested in IE6, IE7, IE8, FF3.6 and Chrome 5. Usage: | ||
@@ -96,2 +127,3 @@ ```html | ||
* [doUntil](#doUntil) | ||
* [forever](#forever) | ||
* [waterfall](#waterfall) | ||
@@ -345,3 +377,3 @@ * [compose](#compose) | ||
each step in the reduction needs to be async, if you can get the data before | ||
reducing it then its probably a good idea to do so. | ||
reducing it then it's probably a good idea to do so. | ||
@@ -761,3 +793,11 @@ __Arguments__ | ||
--------------------------------------- | ||
<a name="forever" /> | ||
### forever(fn, callback) | ||
Calls the asynchronous function 'fn' repeatedly, in series, indefinitely. | ||
If an error is passed to fn's callback then 'callback' is called with the | ||
error, otherwise it will never be called. | ||
--------------------------------------- | ||
@@ -873,2 +913,9 @@ | ||
<a name="applyEachSeries" /> | ||
### applyEachSeries(arr, iterator, callback) | ||
The same as applyEach only the functions are applied in series. | ||
--------------------------------------- | ||
<a name="queue" /> | ||
@@ -957,7 +1004,7 @@ ### queue(worker, concurrency) | ||
* worker(tasks, callback) - An asynchronous function for processing queued | ||
tasks, which must call its callback(err) argument when finished, with an | ||
optional error as an argument. | ||
* worker(tasks, callback) - An asynchronous function for processing an array of | ||
queued tasks, which must call its callback(err) argument when finished, with | ||
an optional error as an argument. | ||
* payload - An optional integer for determining how many tasks should be | ||
process per round, default is unlimited. | ||
processed per round; if omitted, the default is unlimited. | ||
@@ -985,4 +1032,6 @@ __Cargo objects__ | ||
var cargo = async.cargo(function (task, callback) { | ||
console.log('hello ' + task.name); | ||
var cargo = async.cargo(function (tasks, callback) { | ||
for(var i=0; i<tasks.length; i++){ | ||
console.log('hello ' + tasks[i].name); | ||
} | ||
callback(); | ||
@@ -1121,3 +1170,3 @@ }, 2); | ||
Creates an iterator function which calls the next function in the array, | ||
returning a continuation to call the next one after that. Its also possible to | ||
returning a continuation to call the next one after that. It's also possible to | ||
'peek' the next iterator by doing iterator.next(). | ||
@@ -1124,0 +1173,0 @@ |
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
73774
1415
882