File tree 2 files changed +51
-16
lines changed
2 files changed +51
-16
lines changed Original file line number Diff line number Diff line change @@ -131,30 +131,29 @@ function isPromiseLike(maybePromise) {
131
131
* @param {function } callback The callback called after every item has been iterated
132
132
*/
133
133
function eachAsync ( arr , eachFn , callback ) {
134
- if ( arr . length === 0 ) {
135
- callback ( null ) ;
134
+ arr = arr || [ ] ;
135
+
136
+ let idx = 0 ;
137
+ let awaiting = 0 ;
138
+ for ( idx = 0 ; idx < arr . length ; ++ idx ) {
139
+ awaiting ++ ;
140
+ eachFn ( arr [ idx ] , eachCallback ) ;
141
+ }
142
+
143
+ if ( awaiting === 0 ) {
144
+ callback ( ) ;
136
145
return ;
137
146
}
138
147
139
- const length = arr . length ;
140
- let completed = 0 ;
141
148
function eachCallback ( err ) {
149
+ awaiting -- ;
142
150
if ( err ) {
143
- callback ( err , null ) ;
151
+ callback ( err ) ;
144
152
return ;
145
153
}
146
154
147
- if ( ++ completed === length ) {
148
- callback ( null ) ;
149
- }
150
- }
151
-
152
- for ( let idx = 0 ; idx < length ; ++ idx ) {
153
- try {
154
- eachFn ( arr [ idx ] , eachCallback ) ;
155
- } catch ( err ) {
156
- callback ( err ) ;
157
- return ;
155
+ if ( idx === arr . length && awaiting <= 0 ) {
156
+ callback ( ) ;
158
157
}
159
158
}
160
159
}
Original file line number Diff line number Diff line change
1
+ 'use strict' ;
2
+ const eachAsync = require ( '../../lib/core/utils' ) . eachAsync ;
3
+ const expect = require ( 'chai' ) . expect ;
4
+
5
+ describe ( 'utils' , function ( ) {
6
+ describe ( 'eachAsync' , function ( ) {
7
+ it ( 'should callback with an error' , function ( done ) {
8
+ eachAsync (
9
+ [ { error : false } , { error : true } ] ,
10
+ ( item , cb ) => {
11
+ cb ( item . error ? new Error ( 'error requested' ) : null ) ;
12
+ } ,
13
+ err => {
14
+ expect ( err ) . to . exist ;
15
+ done ( ) ;
16
+ }
17
+ ) ;
18
+ } ) ;
19
+
20
+ it ( 'should propagate a synchronously thrown error' , function ( done ) {
21
+ expect ( ( ) =>
22
+ eachAsync (
23
+ [ { } ] ,
24
+ ( ) => {
25
+ throw new Error ( 'something wicked' ) ;
26
+ } ,
27
+ err => {
28
+ expect ( err ) . to . not . exist ;
29
+ done ( err ) ;
30
+ }
31
+ )
32
+ ) . to . throw ( / s o m e t h i n g w i c k e d / ) ;
33
+ done ( ) ;
34
+ } ) ;
35
+ } ) ;
36
+ } ) ;
You can’t perform that action at this time.
0 commit comments