Skip to content

Commit 2a4a42c

Browse files
committed
fix(bulkWrite): fix issue with bulkWrite continuing w/ callback
1 parent b86ee69 commit 2a4a42c

File tree

3 files changed

+17
-12
lines changed

3 files changed

+17
-12
lines changed

lib/bulk/common.js

+15-10
Original file line numberDiff line numberDiff line change
@@ -906,6 +906,15 @@ class BulkOperationBase {
906906
);
907907
}
908908

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+
909918
/**
910919
* Execute next write command in a chain
911920
*
@@ -919,19 +928,17 @@ class BulkOperationBase {
919928
if (typeof options === 'function') (callback = options), (options = {});
920929
options = options || {};
921930

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-
929931
if (typeof _writeConcern === 'function') {
930932
callback = _writeConcern;
931933
} else if (_writeConcern && typeof _writeConcern === 'object') {
932934
this.s.writeConcern = _writeConcern;
933935
}
934936

937+
if (this.s.executed) {
938+
const executedError = toError('batch cannot be re-executed');
939+
return this._handleEarlyError(executedError, callback);
940+
}
941+
935942
// If we have current batch
936943
if (this.isOrdered) {
937944
if (this.s.currentBatch) this.s.batches.push(this.s.currentBatch);
@@ -943,9 +950,7 @@ class BulkOperationBase {
943950
// If we have no operations in the bulk raise an error
944951
if (this.s.batches.length === 0) {
945952
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);
949954
}
950955
return { options, callback };
951956
}

lib/bulk/ordered.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ class OrderedBulkOperation extends BulkOperationBase {
119119
*/
120120
execute(_writeConcern, options, callback) {
121121
const ret = this.bulkExecute(_writeConcern, options, callback);
122-
if (isPromiseLike(ret)) {
122+
if (!ret || isPromiseLike(ret)) {
123123
return ret;
124124
}
125125

lib/bulk/unordered.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ class UnorderedBulkOperation extends BulkOperationBase {
131131
*/
132132
execute(_writeConcern, options, callback) {
133133
const ret = this.bulkExecute(_writeConcern, options, callback);
134-
if (isPromiseLike(ret)) {
134+
if (!ret || isPromiseLike(ret)) {
135135
return ret;
136136
}
137137

0 commit comments

Comments
 (0)