Skip to content

Commit 15c44f1

Browse files
committed
Fix multiple error bug
This commit fixes a bug where the done callback may be called more than once.
1 parent 1a80c79 commit 15c44f1

File tree

3 files changed

+63
-4
lines changed

3 files changed

+63
-4
lines changed

lib/node_modules/@stdlib/utils/count-by-async/lib/limit.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -85,14 +85,14 @@ function limit( collection, opts, indicator, done ) {
8585
* @returns {void}
8686
*/
8787
function cb( error, group ) {
88-
if ( error ) {
89-
flg = true;
90-
return clbk( error );
91-
}
9288
if ( flg ) {
9389
// Prevent further processing of collection elements:
9490
return;
9591
}
92+
if ( error ) {
93+
flg = true;
94+
return clbk( error );
95+
}
9696
debug( 'Collection element %d group: %s.', j, group );
9797

9898
// Checking for an "own" property is necessary to guard against the edge case where an indicator function returns a group identifier which matches a method or property on the `Object` prototype.

lib/node_modules/@stdlib/utils/count-by-async/test/test.count_by.js

+28
Original file line numberDiff line numberDiff line change
@@ -845,6 +845,34 @@ tape( 'if an error is encountered while processing a collection element, the fun
845845
}
846846
});
847847

848+
tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent)', function test( t ) {
849+
var count;
850+
var arr;
851+
852+
arr = [ 500, 500, 500 ];
853+
count = 0;
854+
countByAsync( arr, indicator, done );
855+
856+
function indicator( value, index, next ) {
857+
count += 1;
858+
setTimeout( onTimeout, value );
859+
860+
function onTimeout() {
861+
return next( new Error( 'beep' ) );
862+
}
863+
}
864+
865+
function done( error ) {
866+
t.strictEqual( count, 3, 'suspends execution' );
867+
if ( error ) {
868+
t.pass( error.message );
869+
} else {
870+
t.fail( 'did not return an error' );
871+
}
872+
t.end();
873+
}
874+
});
875+
848876
tape( 'if provided an empty collection, the function never invokes an indicator function and returns an empty object', function test( t ) {
849877
var arr = [];
850878
countByAsync( arr, indicator, done );

lib/node_modules/@stdlib/utils/count-by-async/test/test.factory.js

+31
Original file line numberDiff line numberDiff line change
@@ -958,6 +958,37 @@ tape( 'if an error is encountered while processing a collection element, the ret
958958
}
959959
});
960960

961+
tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent)', function test( t ) {
962+
var countByAsync;
963+
var count;
964+
var arr;
965+
966+
countByAsync = factory( indicator );
967+
968+
arr = [ 500, 500, 500 ];
969+
count = 0;
970+
countByAsync( arr, done );
971+
972+
function indicator( value, index, next ) {
973+
count += 1;
974+
setTimeout( onTimeout, value );
975+
976+
function onTimeout() {
977+
return next( new Error( 'beep' ) );
978+
}
979+
}
980+
981+
function done( error ) {
982+
t.strictEqual( count, 3, 'suspends execution' );
983+
if ( error ) {
984+
t.pass( error.message );
985+
} else {
986+
t.fail( 'did not return an error' );
987+
}
988+
t.end();
989+
}
990+
});
991+
961992
tape( 'if provided an empty collection, the returned function never invokes an indicator function and returns an empty object', function test( t ) {
962993
var countByAsync;
963994
var arr;

0 commit comments

Comments
 (0)