@@ -906,6 +906,15 @@ class BulkOperationBase {
906
906
) ;
907
907
}
908
908
909
+ _handleEarlyError ( err , callback ) {
910
+ if ( typeof callback === 'function' ) {
911
+ callback ( err , null ) ;
912
+ return ;
913
+ }
914
+
915
+ return this . s . promiseLibrary . reject ( err ) ;
916
+ }
917
+
909
918
/**
910
919
* Execute next write command in a chain
911
920
*
@@ -919,19 +928,17 @@ class BulkOperationBase {
919
928
if ( typeof options === 'function' ) ( callback = options ) , ( options = { } ) ;
920
929
options = options || { } ;
921
930
922
- if ( this . s . executed ) {
923
- const executedError = toError ( 'batch cannot be re-executed' ) ;
924
- return typeof callback === 'function'
925
- ? callback ( executedError , null )
926
- : this . s . promiseLibrary . reject ( executedError ) ;
927
- }
928
-
929
931
if ( typeof _writeConcern === 'function' ) {
930
932
callback = _writeConcern ;
931
933
} else if ( _writeConcern && typeof _writeConcern === 'object' ) {
932
934
this . s . writeConcern = _writeConcern ;
933
935
}
934
936
937
+ if ( this . s . executed ) {
938
+ const executedError = toError ( 'batch cannot be re-executed' ) ;
939
+ return this . _handleEarlyError ( executedError , callback ) ;
940
+ }
941
+
935
942
// If we have current batch
936
943
if ( this . isOrdered ) {
937
944
if ( this . s . currentBatch ) this . s . batches . push ( this . s . currentBatch ) ;
@@ -943,9 +950,7 @@ class BulkOperationBase {
943
950
// If we have no operations in the bulk raise an error
944
951
if ( this . s . batches . length === 0 ) {
945
952
const emptyBatchError = toError ( 'Invalid Operation, no operations specified' ) ;
946
- return typeof callback === 'function'
947
- ? callback ( emptyBatchError , null )
948
- : this . s . promiseLibrary . reject ( emptyBatchError ) ;
953
+ return this . _handleEarlyError ( emptyBatchError , callback ) ;
949
954
}
950
955
return { options, callback } ;
951
956
}
0 commit comments