@@ -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,35 @@ 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
+ return maybePromise ( this , callback , done => {
130
+ if ( this . hasEnded ) {
131
+ return done ( ) ;
132
+ }
132
133
133
- if ( this . serverSession && this . inTransaction ( ) ) {
134
- this . abortTransaction ( ) ; // pass in callback?
135
- }
134
+ function completeEndSession ( ) {
135
+ // release the server session back to the pool
136
+ this . sessionPool . release ( this . serverSession ) ;
137
+ this [ kServerSession ] = undefined ;
136
138
137
- // release the server session back to the pool
138
- this . sessionPool . release ( this . serverSession ) ;
139
- this [ kServerSession ] = undefined ;
139
+ // mark the session as ended, and emit a signal
140
+ this . hasEnded = true ;
141
+ this . emit ( 'ended' , this ) ;
142
+
143
+ // spec indicates that we should ignore all errors for `endSessions`
144
+ done ( ) ;
145
+ }
146
+
147
+ if ( this . serverSession && this . inTransaction ( ) ) {
148
+ this . abortTransaction ( err => {
149
+ if ( err ) return done ( err ) ;
150
+ completeEndSession ( ) ;
151
+ } ) ;
140
152
141
- // mark the session as ended, and emit a signal
142
- this . hasEnded = true ;
143
- this . emit ( 'ended' , this ) ;
153
+ return ;
154
+ }
144
155
145
- // spec indicates that we should ignore all errors for `endSessions`
146
- if ( typeof callback === 'function' ) callback ( null , null ) ;
156
+ completeEndSession ( ) ;
157
+ } ) ;
147
158
}
148
159
149
160
/**
@@ -227,16 +238,7 @@ class ClientSession extends EventEmitter {
227
238
* @return {Promise } A promise is returned if no callback is provided
228
239
*/
229
240
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
- } ) ;
241
+ return maybePromise ( this , callback , done => endTransaction ( this , 'commitTransaction' , done ) ) ;
240
242
}
241
243
242
244
/**
@@ -246,16 +248,7 @@ class ClientSession extends EventEmitter {
246
248
* @return {Promise } A promise is returned if no callback is provided
247
249
*/
248
250
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
- } ) ;
251
+ return maybePromise ( this , callback , done => endTransaction ( this , 'abortTransaction' , done ) ) ;
259
252
}
260
253
261
254
/**
0 commit comments