summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGraeme Yeates <yeatesgraeme@gmail.com>2016-02-08 22:11:02 -0500
committerGraeme Yeates <yeatesgraeme@gmail.com>2016-02-08 22:11:02 -0500
commit01205e053b16c7843a3d4d3c9075b33de5adb2d0 (patch)
tree9d9cf53d3913f17519eb4634bf58b9c3a548545a
parentc8ae0e0846b144c2f5e79c737082c7472ce56d0a (diff)
parentf919b0fa055b473006bc4026730be7a6c8f52487 (diff)
downloadasync-01205e053b16c7843a3d4d3c9075b33de5adb2d0.tar.gz
Merge pull request #1021 from crispen-smith/master
Update README.md
-rw-r--r--README.md192
1 files changed, 96 insertions, 96 deletions
diff --git a/README.md b/README.md
index c7c1efc..d731b82 100644
--- a/README.md
+++ b/README.md
@@ -54,12 +54,12 @@ missing please create a GitHub issue for it.
## Common Pitfalls <sub>[(StackOverflow)](http://stackoverflow.com/questions/tagged/async.js)</sub>
### Synchronous iteration functions
-If you get an error like `RangeError: Maximum call stack size exceeded.` or other stack overflow issues when using async, you are likely using a synchronous iterator. By *synchronous* we mean a function that calls its callback on the same tick in the javascript event loop, without doing any I/O or using any timers. Calling many callbacks iteratively will quickly overflow the stack. If you run into this issue, just defer your callback with `async.setImmediate` to start a new call stack on the next tick of the event loop.
+If you get an error like `RangeError: Maximum call stack size exceeded.` or other stack overflow issues when using async, you are likely using a synchronous iteratee. By *synchronous* we mean a function that calls its callback on the same tick in the javascript event loop, without doing any I/O or using any timers. Calling many callbacks iteratively will quickly overflow the stack. If you run into this issue, just defer your callback with `async.setImmediate` to start a new call stack on the next tick of the event loop.
This can also arise by accident if you callback early in certain cases:
```js
-async.eachSeries(hugeArray, function iterator(item, callback) {
+async.eachSeries(hugeArray, function iteratee(item, callback) {
if (inCache(item)) {
callback(null, cache[item]); // if many items are cached, you'll overflow
} else {
@@ -73,7 +73,7 @@ async.eachSeries(hugeArray, function iterator(item, callback) {
Just change it to:
```js
-async.eachSeries(hugeArray, function iterator(item, callback) {
+async.eachSeries(hugeArray, function iteratee(item, callback) {
if (inCache(item)) {
async.setImmediate(function () {
callback(null, cache[item]);
@@ -112,11 +112,11 @@ async.waterfall([
It is always good practice to `return callback(err, result)` whenever a callback call is not the last statement of a function.
-### Binding a context to an iterator
+### Binding a context to an iteratee
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:
+make `async` execute your iteratees in a given context, or are confused as to why
+a method of another library isn't working as an iteratee, study this example:
```js
// Here is a simple object with an (unnecessarily roundabout) squaring method
@@ -139,7 +139,7 @@ async.map([1, 2, 3], AsyncSquaringLibrary.square, function(err, result){
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
+ // With the help of bind we can attach a context to the iteratee 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.
@@ -234,25 +234,25 @@ Some functions are also available in the following forms:
<a name="forEach"></a>
<a name="each"></a>
-### each(arr, iterator, [callback])
+### each(arr, iteratee, [callback])
-Applies the function `iterator` to each item in `arr`, in parallel.
-The `iterator` is called with an item from the list, and a callback for when it
-has finished. If the `iterator` passes an error to its `callback`, the main
+Applies the function `iteratee` to each item in `arr`, in parallel.
+The `iteratee` is called with an item from the list, and a callback for when it
+has finished. If the `iteratee` passes an error to its `callback`, the main
`callback` (for the `each` function) is immediately called with the error.
-Note, that since this function applies `iterator` to each item in parallel,
-there is no guarantee that the iterator functions will complete in order.
+Note, that since this function applies `iteratee` to each item in parallel,
+there is no guarantee that the iteratee functions will complete in order.
__Arguments__
* `arr` - An array to iterate over.
-* `iterator(item, callback)` - A function to apply to each item in `arr`.
- The iterator is passed a `callback(err)` which must be called once it has
+* `iteratee(item, callback)` - A function to apply to each item in `arr`.
+ The iteratee is passed a `callback(err)` which must be called once it has
completed. If no error has occurred, the `callback` should be run without
arguments or with an explicit `null` argument. The array index is not passed
- to the iterator. If you need the index, use [`forEachOf`](#forEachOf).
-* `callback(err)` - *Optional* A callback which is called when all `iterator` functions
+ to the iteratee. If you need the index, use [`forEachOf`](#forEachOf).
+* `callback(err)` - *Optional* A callback which is called when all `iteratee` functions
have finished, or an error occurs.
__Examples__
@@ -297,26 +297,26 @@ async.each(openFiles, function(file, callback) {
__Related__
-* eachSeries(arr, iterator, [callback])
-* eachLimit(arr, limit, iterator, [callback])
+* eachSeries(arr, iteratee, [callback])
+* eachLimit(arr, limit, iteratee, [callback])
---------------------------------------
<a name="forEachOf"></a>
<a name="eachOf"></a>
-### forEachOf(obj, iterator, [callback])
+### forEachOf(obj, iteratee, [callback])
-Like `each`, except that it iterates over objects, and passes the key as the second argument to the iterator.
+Like `each`, except that it iterates over objects, and passes the key as the second argument to the iteratee.
__Arguments__
* `obj` - An object or array to iterate over.
-* `iterator(item, key, callback)` - A function to apply to each item in `obj`.
-The `key` is the item's key, or index in the case of an array. The iterator is
+* `iteratee(item, key, callback)` - A function to apply to each item in `obj`.
+The `key` is the item's key, or index in the case of an array. The iteratee is
passed a `callback(err)` which must be called once it has completed. If no
error has occurred, the callback should be run without arguments or with an
explicit `null` argument.
-* `callback(err)` - *Optional* A callback which is called when all `iterator` functions have finished, or an error occurs.
+* `callback(err)` - *Optional* A callback which is called when all `iteratee` functions have finished, or an error occurs.
__Example__
@@ -343,31 +343,31 @@ async.forEachOf(obj, function (value, key, callback) {
__Related__
-* forEachOfSeries(obj, iterator, [callback])
-* forEachOfLimit(obj, limit, iterator, [callback])
+* forEachOfSeries(obj, iteratee, [callback])
+* forEachOfLimit(obj, limit, iteratee, [callback])
---------------------------------------
<a name="map"></a>
-### map(arr, iterator, [callback])
+### map(arr, iteratee, [callback])
Produces a new array of values by mapping each value in `arr` through
-the `iterator` function. The `iterator` is called with an item from `arr` and a
+the `iteratee` function. The `iteratee` is called with an item from `arr` and a
callback for when it has finished processing. Each of these callback takes 2 arguments:
-an `error`, and the transformed item from `arr`. If `iterator` passes an error to its
+an `error`, and the transformed item from `arr`. If `iteratee` passes an error to its
callback, the main `callback` (for the `map` function) is immediately called with the error.
-Note, that since this function applies the `iterator` to each item in parallel,
-there is no guarantee that the `iterator` functions will complete in order.
+Note, that since this function applies the `iteratee` to each item in parallel,
+there is no guarantee that the `iteratee` functions will complete in order.
However, the results array will be in the same order as the original `arr`.
__Arguments__
* `arr` - An array to iterate over.
-* `iterator(item, callback)` - A function to apply to each item in `arr`.
- The iterator is passed a `callback(err, transformed)` which must be called once
+* `iteratee(item, callback)` - A function to apply to each item in `arr`.
+ The iteratee is passed a `callback(err, transformed)` which must be called once
it has completed with an error (which can be `null`) and a transformed item.
-* `callback(err, results)` - *Optional* A callback which is called when all `iterator`
+* `callback(err, results)` - *Optional* A callback which is called when all `iteratee`
functions have finished, or an error occurs. Results is an array of the
transformed items from the `arr`.
@@ -381,19 +381,19 @@ async.map(['file1','file2','file3'], fs.stat, function(err, results){
__Related__
-* mapSeries(arr, iterator, [callback])
-* mapLimit(arr, limit, iterator, [callback])
+* mapSeries(arr, iteratee, [callback])
+* mapLimit(arr, limit, iteratee, [callback])
---------------------------------------
<a name="select"></a>
<a name="filter"></a>
-### filter(arr, iterator, [callback])
+### filter(arr, iteratee, [callback])
__Alias:__ `select`
Returns a new array of all the values in `arr` which pass an async truth test.
-_The callback for each `iterator` call only accepts a single argument of `true` or
+_The callback for each `iteratee` call only accepts a single argument of `true` or
`false`; it does not accept an error argument first!_ This is in-line with the
way node libraries work with truth tests like `fs.exists`. This operation is
performed in parallel, but the results array will be in the same order as the
@@ -402,10 +402,10 @@ original.
__Arguments__
* `arr` - An array to iterate over.
-* `iterator(item, callback)` - A truth test to apply to each item in `arr`.
- The `iterator` is passed a `callback(truthValue)`, which must be called with a
+* `iteratee(item, callback)` - A truth test to apply to each item in `arr`.
+ The `iteratee` is passed a `callback(truthValue)`, which must be called with a
boolean argument once it has completed.
-* `callback(results)` - *Optional* A callback which is called after all the `iterator`
+* `callback(results)` - *Optional* A callback which is called after all the `iteratee`
functions have finished.
__Example__
@@ -418,29 +418,29 @@ async.filter(['file1','file2','file3'], fs.exists, function(results){
__Related__
-* filterSeries(arr, iterator, [callback])
-* filterLimit(arr, limit, iterator, [callback])
+* filterSeries(arr, iteratee, [callback])
+* filterLimit(arr, limit, iteratee, [callback])
---------------------------------------
<a name="reject"></a>
-### reject(arr, iterator, [callback])
+### reject(arr, iteratee, [callback])
The opposite of [`filter`](#filter). Removes values that pass an `async` truth test.
__Related__
-* rejectSeries(arr, iterator, [callback])
-* rejectLimit(arr, limit, iterator, [callback])
+* rejectSeries(arr, iteratee, [callback])
+* rejectLimit(arr, limit, iteratee, [callback])
---------------------------------------
<a name="reduce"></a>
-### reduce(arr, memo, iterator, [callback])
+### reduce(arr, memo, iteratee, [callback])
__Aliases:__ `inject`, `foldl`
-Reduces `arr` into a single value using an async `iterator` to return
+Reduces `arr` into a single value using an async `iteratee` to return
each successive step. `memo` is the initial state of the reduction.
This function only operates in series.
@@ -453,13 +453,13 @@ __Arguments__
* `arr` - An array to iterate over.
* `memo` - The initial state of the reduction.
-* `iterator(memo, item, callback)` - A function applied to each item in the
- array to produce the next step in the reduction. The `iterator` is passed a
+* `iteratee(memo, item, callback)` - A function applied to each item in the
+ array to produce the next step in the reduction. The `iteratee` is passed a
`callback(err, reduction)` which accepts an optional error as its first
argument, and the state of the reduction as the second. If an error is
passed to the callback, the reduction is stopped and the main `callback` is
immediately called with the error.
-* `callback(err, result)` - *Optional* A callback which is called after all the `iterator`
+* `callback(err, result)` - *Optional* A callback which is called after all the `iteratee`
functions have finished. Result is the reduced value.
__Example__
@@ -478,7 +478,7 @@ async.reduce([1,2,3], 0, function(memo, item, callback){
---------------------------------------
<a name="reduceRight"></a>
-### reduceRight(arr, memo, iterator, [callback])
+### reduceRight(arr, memo, iteratee, [callback])
__Alias:__ `foldr`
@@ -487,10 +487,10 @@ Same as [`reduce`](#reduce), only operates on `arr` in reverse order.
---------------------------------------
<a name="detect"></a>
-### detect(arr, iterator, [callback])
+### detect(arr, iteratee, [callback])
Returns the first value in `arr` that passes an async truth test. The
-`iterator` is applied in parallel, meaning the first iterator to return `true` will
+`iteratee` is applied in parallel, meaning the first iteratee to return `true` will
fire the detect `callback` with that result. That means the result might not be
the first item in the original `arr` (in terms of order) that passes the test.
@@ -499,12 +499,12 @@ If order within the original `arr` is important, then look at `detectSeries`.
__Arguments__
* `arr` - An array to iterate over.
-* `iterator(item, callback)` - A truth test to apply to each item in `arr`.
- The iterator is passed a `callback(truthValue)` which must be called with a
+* `iteratee(item, callback)` - A truth test to apply to each item in `arr`.
+ The iteratee is passed a `callback(truthValue)` which must be called with a
boolean argument once it has completed. **Note: this callback does not take an error as its first argument.**
-* `callback(result)` - *Optional* A callback which is called as soon as any iterator returns
- `true`, or after all the `iterator` functions have finished. Result will be
- the first item in the array that passes the truth test (iterator) or the
+* `callback(result)` - *Optional* A callback which is called as soon as any iteratee returns
+ `true`, or after all the `iteratee` functions have finished. Result will be
+ the first item in the array that passes the truth test (iteratee) or the
value `undefined` if none passed. **Note: this callback does not take an error as its first argument.**
__Example__
@@ -517,26 +517,26 @@ async.detect(['file1','file2','file3'], fs.exists, function(result){
__Related__
-* detectSeries(arr, iterator, [callback])
-* detectLimit(arr, limit, iterator, [callback])
+* detectSeries(arr, iteratee, [callback])
+* detectLimit(arr, limit, iteratee, [callback])
---------------------------------------
<a name="sortBy"></a>
-### sortBy(arr, iterator, [callback])
+### sortBy(arr, iteratee, [callback])
-Sorts a list by the results of running each `arr` value through an async `iterator`.
+Sorts a list by the results of running each `arr` value through an async `iteratee`.
__Arguments__
* `arr` - An array to iterate over.
-* `iterator(item, callback)` - A function to apply to each item in `arr`.
- The iterator is passed a `callback(err, sortValue)` which must be called once it
+* `iteratee(item, callback)` - A function to apply to each item in `arr`.
+ The iteratee is passed a `callback(err, sortValue)` which must be called once it
has completed with an error (which can be `null`) and a value to use as the sort
criteria.
-* `callback(err, results)` - *Optional* A callback which is called after all the `iterator`
+* `callback(err, results)` - *Optional* A callback which is called after all the `iteratee`
functions have finished, or an error occurs. Results is the items from
- the original `arr` sorted by the values returned by the `iterator` calls.
+ the original `arr` sorted by the values returned by the `iteratee` calls.
__Example__
@@ -574,24 +574,24 @@ async.sortBy([1,9,3,5], function(x, callback){
---------------------------------------
<a name="some"></a>
-### some(arr, iterator, [callback])
+### some(arr, iteratee, [callback])
__Alias:__ `any`
Returns `true` if at least one element in the `arr` satisfies an async test.
-_The callback for each iterator call only accepts a single argument of `true` or
+_The callback for each iteratee call only accepts a single argument of `true` or
`false`; it does not accept an error argument first!_ This is in-line with the
-way node libraries work with truth tests like `fs.exists`. Once any iterator
+way node libraries work with truth tests like `fs.exists`. Once any iteratee
call returns `true`, the main `callback` is immediately called.
__Arguments__
* `arr` - An array to iterate over.
-* `iterator(item, callback)` - A truth test to apply to each item in the array
- in parallel. The iterator is passed a `callback(truthValue)`` which must be
+* `iteratee(item, callback)` - A truth test to apply to each item in the array
+ in parallel. The iteratee is passed a `callback(truthValue)`` which must be
called with a boolean argument once it has completed.
-* `callback(result)` - *Optional* A callback which is called as soon as any iterator returns
- `true`, or after all the iterator functions have finished. Result will be
+* `callback(result)` - *Optional* A callback which is called as soon as any iteratee returns
+ `true`, or after all the iteratee functions have finished. Result will be
either `true` or `false` depending on the values of the async tests.
**Note: the callbacks do not take an error as their first argument.**
@@ -605,28 +605,28 @@ async.some(['file1','file2','file3'], fs.exists, function(result){
__Related__
-* someLimit(arr, limit, iterator, callback)
+* someLimit(arr, limit, iteratee, callback)
---------------------------------------
<a name="every"></a>
-### every(arr, iterator, [callback])
+### every(arr, iteratee, [callback])
__Alias:__ `all`
Returns `true` if every element in `arr` satisfies an async test.
-_The callback for each `iterator` call only accepts a single argument of `true` or
+_The callback for each `iteratee` call only accepts a single argument of `true` or
`false`; it does not accept an error argument first!_ This is in-line with the
way node libraries work with truth tests like `fs.exists`.
__Arguments__
* `arr` - An array to iterate over.
-* `iterator(item, callback)` - A truth test to apply to each item in the array
- in parallel. The iterator is passed a `callback(truthValue)` which must be
+* `iteratee(item, callback)` - A truth test to apply to each item in the array
+ in parallel. The iteratee is passed a `callback(truthValue)` which must be
called with a boolean argument once it has completed.
-* `callback(result)` - *Optional* A callback which is called as soon as any iterator returns
- `false`, or after all the iterator functions have finished. Result will be
+* `callback(result)` - *Optional* A callback which is called as soon as any iteratee returns
+ `false`, or after all the iteratee functions have finished. Result will be
either `true` or `false` depending on the values of the async tests.
**Note: the callbacks do not take an error as their first argument.**
@@ -641,27 +641,27 @@ async.every(['file1','file2','file3'], fs.exists, function(result){
__Related__
-* everyLimit(arr, limit, iterator, callback)
+* everyLimit(arr, limit, iteratee, callback)
---------------------------------------
<a name="concat"></a>
-### concat(arr, iterator, [callback])
+### concat(arr, iteratee, [callback])
-Applies `iterator` to each item in `arr`, concatenating the results. Returns the
-concatenated list. The `iterator`s are called in parallel, and the results are
+Applies `iteratee` to each item in `arr`, concatenating the results. Returns the
+concatenated list. The `iteratee`s are called in parallel, and the results are
concatenated as they return. There is no guarantee that the results array will
-be returned in the original order of `arr` passed to the `iterator` function.
+be returned in the original order of `arr` passed to the `iteratee` function.
__Arguments__
* `arr` - An array to iterate over.
-* `iterator(item, callback)` - A function to apply to each item in `arr`.
- The iterator is passed a `callback(err, results)` which must be called once it
+* `iteratee(item, callback)` - A function to apply to each item in `arr`.
+ The iteratee is passed a `callback(err, results)` which must be called once it
has completed with an error (which can be `null`) and an array of results.
-* `callback(err, results)` - *Optional* A callback which is called after all the `iterator`
+* `callback(err, results)` - *Optional* A callback which is called after all the `iteratee`
functions have finished, or an error occurs. Results is an array containing
- the concatenated results of the `iterator` function.
+ the concatenated results of the `iteratee` function.
__Example__
@@ -673,7 +673,7 @@ async.concat(['dir1','dir2','dir3'], fs.readdir, function(err, files){
__Related__
-* concatSeries(arr, iterator, [callback])
+* concatSeries(arr, iteratee, [callback])
## Control Flow
@@ -1606,15 +1606,15 @@ call_order.push('one')
---------------------------------------
<a name="times"></a>
-### times(n, iterator, [callback])
+### times(n, iteratee, [callback])
-Calls the `iterator` function `n` times, and accumulates results in the same manner
+Calls the `iteratee` function `n` times, and accumulates results in the same manner
you would use with [`map`](#map).
__Arguments__
* `n` - The number of times to run the function.
-* `iterator` - The function to call `n` times.
+* `iteratee` - The function to call `n` times.
* `callback` - see [`map`](#map)
__Example__
@@ -1638,8 +1638,8 @@ async.times(5, function(n, next){
__Related__
-* timesSeries(n, iterator, [callback])
-* timesLimit(n, limit, iterator, [callback])
+* timesSeries(n, iteratee, [callback])
+* timesLimit(n, limit, iteratee, [callback])
---------------------------------------