@@ -13,6 +13,7 @@ const Transaction = require('./transactions').Transaction;
13
13
const TxnState = require ( './transactions' ) . TxnState ;
14
14
const isPromiseLike = require ( './utils' ) . isPromiseLike ;
15
15
const ReadPreference = require ( './topologies/read_preference' ) ;
16
+ const maybePromise = require ( '../utils' ) . maybePromise ;
16
17
const isTransactionCommand = require ( './transactions' ) . isTransactionCommand ;
17
18
const resolveClusterTime = require ( './topologies/shared' ) . resolveClusterTime ;
18
19
const isSharded = require ( './wireprotocol/shared' ) . isSharded ;
@@ -125,25 +126,36 @@ class ClientSession extends EventEmitter {
125
126
if ( typeof options === 'function' ) ( callback = options ) , ( options = { } ) ;
126
127
options = options || { } ;
127
128
128
- if ( this . hasEnded ) {
129
- if ( typeof callback === 'function' ) callback ( null , null ) ;
130
- return ;
131
- }
129
+ const session = this ;
130
+ return maybePromise ( this , callback , done => {
131
+ if ( session . hasEnded ) {
132
+ return done ( ) ;
133
+ }
132
134
133
- if ( this . serverSession && this . inTransaction ( ) ) {
134
- this . abortTransaction ( ) ; // pass in callback?
135
- }
135
+ function completeEndSession ( ) {
136
+ // release the server session back to the pool
137
+ session . sessionPool . release ( session . serverSession ) ;
138
+ session [ kServerSession ] = undefined ;
136
139
137
- // release the server session back to the pool
138
- this . sessionPool . release ( this . serverSession ) ;
139
- this [ kServerSession ] = undefined ;
140
+ // mark the session as ended, and emit a signal
141
+ session . hasEnded = true ;
142
+ session . emit ( 'ended' , session ) ;
143
+
144
+ // spec indicates that we should ignore all errors for `endSessions`
145
+ done ( ) ;
146
+ }
147
+
148
+ if ( session . serverSession && session . inTransaction ( ) ) {
149
+ session . abortTransaction ( err => {
150
+ if ( err ) return done ( err ) ;
151
+ completeEndSession ( ) ;
152
+ } ) ;
140
153
141
- // mark the session as ended, and emit a signal
142
- this . hasEnded = true ;
143
- this . emit ( 'ended' , this ) ;
154
+ return ;
155
+ }
144
156
145
- // spec indicates that we should ignore all errors for `endSessions`
146
- if ( typeof callback === 'function' ) callback ( null , null ) ;
157
+ completeEndSession ( ) ;
158
+ } ) ;
147
159
}
148
160
149
161
/**
@@ -227,16 +239,7 @@ class ClientSession extends EventEmitter {
227
239
* @return {Promise } A promise is returned if no callback is provided
228
240
*/
229
241
commitTransaction ( callback ) {
230
- if ( typeof callback === 'function' ) {
231
- endTransaction ( this , 'commitTransaction' , callback ) ;
232
- return ;
233
- }
234
-
235
- return new Promise ( ( resolve , reject ) => {
236
- endTransaction ( this , 'commitTransaction' , ( err , reply ) =>
237
- err ? reject ( err ) : resolve ( reply )
238
- ) ;
239
- } ) ;
242
+ return maybePromise ( this , callback , done => endTransaction ( this , 'commitTransaction' , done ) ) ;
240
243
}
241
244
242
245
/**
@@ -246,16 +249,7 @@ class ClientSession extends EventEmitter {
246
249
* @return {Promise } A promise is returned if no callback is provided
247
250
*/
248
251
abortTransaction ( callback ) {
249
- if ( typeof callback === 'function' ) {
250
- endTransaction ( this , 'abortTransaction' , callback ) ;
251
- return ;
252
- }
253
-
254
- return new Promise ( ( resolve , reject ) => {
255
- endTransaction ( this , 'abortTransaction' , ( err , reply ) =>
256
- err ? reject ( err ) : resolve ( reply )
257
- ) ;
258
- } ) ;
252
+ return maybePromise ( this , callback , done => endTransaction ( this , 'abortTransaction' , done ) ) ;
259
253
}
260
254
261
255
/**
0 commit comments