Skip to content

Commit a976c14

Browse files
committed
fix(execute-operation): return promise on session support check
3.3.0 introduced an extra roundtrip of selection in order to accurately determine if sessions were supported by the topology. Unfortunately, the fix only supported callbacks. So, if a server wasn't actually available immediately, we would return `undefined` in cases when promises were requested for operation execution. NODE-2136
1 parent b3ae4c5 commit a976c14

File tree

1 file changed

+28
-12
lines changed

1 file changed

+28
-12
lines changed

lib/operations/execute_operation.js

+28-12
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,7 @@ function executeOperation(topology, operation, callback) {
3535
!operation.hasAspect(Aspect.SKIP_SESSION) &&
3636
topology.shouldCheckForSessionSupport()
3737
) {
38-
// TODO: this is only supported for unified topology, the first part of this check
39-
// should go away when we drop legacy topology types.
40-
topology.selectServer(ReadPreference.primaryPreferred, err => {
41-
if (err) {
42-
callback(err);
43-
return;
44-
}
45-
46-
executeOperation(topology, operation, callback);
47-
});
48-
49-
return;
38+
return selectServerForSessionSupport(topology, operation, callback);
5039
}
5140

5241
const Promise = topology.s.promiseLibrary;
@@ -179,4 +168,31 @@ function executeWithServerSelection(topology, operation, callback) {
179168
});
180169
}
181170

171+
// TODO: This is only supported for unified topology, it should go away once
172+
// we remove support for legacy topology types.
173+
function selectServerForSessionSupport(topology, operation, callback) {
174+
const Promise = topology.s.promiseLibrary;
175+
176+
let result;
177+
if (typeof callback !== 'function') {
178+
result = new Promise((resolve, reject) => {
179+
callback = (err, result) => {
180+
if (err) return reject(err);
181+
resolve(result);
182+
};
183+
});
184+
}
185+
186+
topology.selectServer(ReadPreference.primaryPreferred, err => {
187+
if (err) {
188+
callback(err);
189+
return;
190+
}
191+
192+
executeOperation(topology, operation, callback);
193+
});
194+
195+
return result;
196+
}
197+
182198
module.exports = executeOperation;

0 commit comments

Comments
 (0)