Permalink
Browse files

Explain how to retrieved number of successful item callbacks

  • Loading branch information...
1 parent 703ae25 commit f1987475deb1746987d457d86685803cf67f767f @wdavidw wdavidw committed Jan 23, 2013
Showing with 19 additions and 4 deletions.
  1. +13 −2 doc/index.md
  2. +2 −0 lib/each.js
  3. +3 −1 src/each.coffee
  4. +1 −1 test/error.coffee
View
@@ -153,7 +153,7 @@ see the code examples below for usage.
Inside array iteration, callback signature is `function([value], [index], next)`
```javascript
-each({})
+each([])
// 1 argument
.on('item', function(next){})
// 2 arguments
@@ -169,7 +169,7 @@ each({})
Inside object iteration, callback signature is `function([key], [value], [counter], next)`
```javascript
-each({})
+each([])
// 1 argument
.on('item', function(next){})
// 2 arguments
@@ -195,6 +195,17 @@ The first argument passed to the `error` event callback is an error instance. In
`sequential` mode, it is always the error that was thrown by the failed item callback. In
`parallel` and `concurrent` modes, there may be more than one event thrown asynchrously. In such case, the error has a generic message such as "Multiple error #{number of errors}" and the property ".errors" give access to each individual error.
+It is possible to know the number of successful item callbacks in the `both` event by substracting the number of run callbacks provided as the second argument to the number of errors provided as the first argument.
+
+```javascript
+each([])
+.on('item', function(next){ next() })
+.on('both', function(err, count){
+ succeed = count - err.errors.lenth
+ console.log('Successful callbacks' + succeed);
+})
+```
+
Traversing an array
-------------------
View
@@ -109,12 +109,14 @@ module.exports = function(elements) {
if (parallel !== 1) {
if (errors.length === 1) {
error = errors[0];
+ error.errors = [];
} else {
error = new Error("Multiple errors (" + errors.length + ")");
error.errors = errors;
}
} else {
error = errors[0];
+ error.errors = [];
}
lerror = events.error.length;
lboth = events.both.length;
View
@@ -80,12 +80,14 @@ module.exports = (elements) ->
if errors.length
if parallel isnt 1
if errors.length is 1
- then error = errors[0]
+ error = errors[0]
+ error.errors = []
else
error = new Error("Multiple errors (#{errors.length})")
error.errors = errors
else
error = errors[0]
+ error.errors = []
lerror = events.error.length
lboth = events.both.length
emitError = lerror or (not lerror and not lboth)
View
@@ -38,7 +38,7 @@ describe 'Error', ->
error_assert = (err) ->
current.should.eql 6
err.message.should.eql 'Testing error in 6'
- should.not.exists err.errors
+ err.errors.length.should.eql 0
each( [ {id: 1}, {id: 2}, {id: 3}, {id: 4}, {id: 5}, {id: 6}, {id: 7}, {id: 8}, {id: 9}, {id: 10}, {id: 11} ] )
.parallel( 4 )
.on 'item', (element, index, next) ->

0 comments on commit f198747

Please sign in to comment.