Skip to content

Commit 31f1bee

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

File tree

3 files changed

+65
-6
lines changed

3 files changed

+65
-6
lines changed

lib/node_modules/@stdlib/utils/inmap-async/lib/limit.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ var debug = require( 'debug' )( 'inmap-async:limit' );
2121
*/
2222
function limit( collection, opts, fcn, done ) {
2323
var maxIndex;
24-
var errFLG;
2524
var count;
25+
var flg;
2626
var lim;
2727
var len;
2828
var idx;
@@ -78,14 +78,14 @@ function limit( collection, opts, fcn, done ) {
7878
* @returns {void}
7979
*/
8080
function cb( error, result ) {
81-
if ( error ) {
82-
errFLG = true;
83-
return clbk( error );
84-
}
85-
if ( errFLG ) {
81+
if ( flg ) {
8682
// Prevent further processing of collection elements:
8783
return;
8884
}
85+
if ( error ) {
86+
flg = true;
87+
return clbk( error );
88+
}
8989
collection[ j ] = result;
9090
clbk();
9191
} // end FUNCTION cb()

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

+31
Original file line numberDiff line numberDiff line change
@@ -814,6 +814,37 @@ tape( 'if an error is encountered while processing a collection element, the ret
814814
}
815815
});
816816

817+
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 ) {
818+
var inmapAsync;
819+
var count;
820+
var arr;
821+
822+
inmapAsync = factory( fcn );
823+
824+
arr = [ 500, 500, 500 ];
825+
count = 0;
826+
inmapAsync( arr, done );
827+
828+
function fcn( value, index, next ) {
829+
count += 1;
830+
setTimeout( onTimeout, value );
831+
832+
function onTimeout() {
833+
return next( new Error( 'beep' ) );
834+
}
835+
}
836+
837+
function done( error ) {
838+
t.strictEqual( count, 3, 'suspends execution' );
839+
if ( error ) {
840+
t.pass( error.message );
841+
} else {
842+
t.fail( 'did not return an error' );
843+
}
844+
t.end();
845+
}
846+
});
847+
817848
tape( 'if an error is encountered while processing a collection element, a collection may be partially mutated', function test( t ) {
818849
var inmapAsync;
819850
var expected;

lib/node_modules/@stdlib/utils/inmap-async/test/test.inmap.js

+28
Original file line numberDiff line numberDiff line change
@@ -722,6 +722,34 @@ tape( 'if an error is encountered while processing a collection element, the fun
722722
}
723723
});
724724

725+
tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent)', function test( t ) {
726+
var count;
727+
var arr;
728+
729+
arr = [ 500, 500, 500 ];
730+
count = 0;
731+
inmapAsync( arr, fcn, done );
732+
733+
function fcn( value, index, next ) {
734+
count += 1;
735+
setTimeout( onTimeout, value );
736+
737+
function onTimeout() {
738+
return next( new Error( 'beep' ) );
739+
}
740+
}
741+
742+
function done( error ) {
743+
t.strictEqual( count, 3, 'suspends execution' );
744+
if ( error ) {
745+
t.pass( error.message );
746+
} else {
747+
t.fail( 'did not return an error' );
748+
}
749+
t.end();
750+
}
751+
});
752+
725753
tape( 'if an error is encountered while processing a collection element, a collection may be partially mutated', function test( t ) {
726754
var expected;
727755
var arr;

0 commit comments

Comments
 (0)