diff --git a/lib/operations/execute_operation.js b/lib/operations/execute_operation.js index b6276f3ddb..e23a80c02e 100644 --- a/lib/operations/execute_operation.js +++ b/lib/operations/execute_operation.js @@ -35,18 +35,7 @@ function executeOperation(topology, operation, callback) { !operation.hasAspect(Aspect.SKIP_SESSION) && topology.shouldCheckForSessionSupport() ) { - // TODO: this is only supported for unified topology, the first part of this check - // should go away when we drop legacy topology types. - topology.selectServer(ReadPreference.primaryPreferred, err => { - if (err) { - callback(err); - return; - } - - executeOperation(topology, operation, callback); - }); - - return; + return selectServerForSessionSupport(topology, operation, callback); } const Promise = topology.s.promiseLibrary; @@ -179,4 +168,31 @@ function executeWithServerSelection(topology, operation, callback) { }); } +// TODO: This is only supported for unified topology, it should go away once +// we remove support for legacy topology types. +function selectServerForSessionSupport(topology, operation, callback) { + const Promise = topology.s.promiseLibrary; + + let result; + if (typeof callback !== 'function') { + result = new Promise((resolve, reject) => { + callback = (err, result) => { + if (err) return reject(err); + resolve(result); + }; + }); + } + + topology.selectServer(ReadPreference.primaryPreferred, err => { + if (err) { + callback(err); + return; + } + + executeOperation(topology, operation, callback); + }); + + return result; +} + module.exports = executeOperation;