Skip to content

Commit 1a80c79

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

File tree

3 files changed

+63
-4
lines changed

3 files changed

+63
-4
lines changed

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -91,14 +91,14 @@ function limit( collection, opts, predicate, done ) {
9191
* @returns {void}
9292
*/
9393
function cb( error, bool ) {
94-
if ( error ) {
95-
flg = true;
96-
return clbk( error );
97-
}
9894
if ( flg ) {
9995
// Prevent further processing of collection elements:
10096
return;
10197
}
98+
if ( error ) {
99+
flg = true;
100+
return clbk( error );
101+
}
102102
debug( 'Collection element %d group: %s.', j, ( bool ) ? '0' : '1' );
103103

104104
// Default is to return values...

lib/node_modules/@stdlib/utils/bifurcate-by-async/test/test.bifurcate_by.js

+28
Original file line numberDiff line numberDiff line change
@@ -890,6 +890,34 @@ tape( 'if an error is encountered while processing a collection element, the fun
890890
}
891891
});
892892

893+
tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent)', function test( t ) {
894+
var count;
895+
var arr;
896+
897+
arr = [ 500, 500, 500 ];
898+
count = 0;
899+
bifurcateByAsync( arr, predicate, done );
900+
901+
function predicate( value, index, next ) {
902+
count += 1;
903+
setTimeout( onTimeout, value );
904+
905+
function onTimeout() {
906+
return next( new Error( 'beep' ) );
907+
}
908+
}
909+
910+
function done( error ) {
911+
t.strictEqual( count, 3, 'suspends execution' );
912+
if ( error ) {
913+
t.pass( error.message );
914+
} else {
915+
t.fail( 'did not return an error' );
916+
}
917+
t.end();
918+
}
919+
});
920+
893921
tape( 'if provided an empty collection, the function never invokes a predicate function and returns an empty array', function test( t ) {
894922
var arr = [];
895923
bifurcateByAsync( arr, predicate, done );

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

+31
Original file line numberDiff line numberDiff line change
@@ -1006,6 +1006,37 @@ tape( 'if an error is encountered while processing a collection element, the ret
10061006
}
10071007
});
10081008

1009+
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 ) {
1010+
var bifurcateByAsync;
1011+
var count;
1012+
var arr;
1013+
1014+
bifurcateByAsync = factory( predicate );
1015+
1016+
arr = [ 500, 500, 500 ];
1017+
count = 0;
1018+
bifurcateByAsync( arr, done );
1019+
1020+
function predicate( value, index, next ) {
1021+
count += 1;
1022+
setTimeout( onTimeout, value );
1023+
1024+
function onTimeout() {
1025+
return next( new Error( 'beep' ) );
1026+
}
1027+
}
1028+
1029+
function done( error ) {
1030+
t.strictEqual( count, 3, 'suspends execution' );
1031+
if ( error ) {
1032+
t.pass( error.message );
1033+
} else {
1034+
t.fail( 'did not return an error' );
1035+
}
1036+
t.end();
1037+
}
1038+
});
1039+
10091040
tape( 'if provided an empty collection, the returned function never invokes a predicate function and returns an empty array', function test( t ) {
10101041
var bifurcateByAsync;
10111042
var arr;

0 commit comments

Comments
 (0)