@@ -273,7 +273,7 @@ export interface MongoClientOptions extends BSONSerializeOptions, SupportedNodeC
273
273
}
274
274
275
275
/** @public */
276
- export type WithSessionCallback = ( session : ClientSession ) => Promise < any > ;
276
+ export type WithSessionCallback < T = any > = ( session : ClientSession ) => Promise < T > ;
277
277
278
278
/** @internal */
279
279
export interface MongoClientPrivate {
@@ -643,12 +643,12 @@ export class MongoClient extends TypedEventEmitter<MongoClientEvents> {
643
643
* @param options - Optional settings for the command
644
644
* @param callback - An callback to execute with an implicitly created session
645
645
*/
646
- withSession ( callback : WithSessionCallback ) : Promise < void > ;
647
- withSession ( options : ClientSessionOptions , callback : WithSessionCallback ) : Promise < void > ;
648
- withSession (
649
- optionsOrOperation ?: ClientSessionOptions | WithSessionCallback ,
650
- callback ?: WithSessionCallback
651
- ) : Promise < void > {
646
+ withSession < T > ( callback : WithSessionCallback < T > ) : Promise < T > ;
647
+ withSession < T > ( options : ClientSessionOptions , callback : WithSessionCallback < T > ) : Promise < T > ;
648
+ withSession < T > (
649
+ optionsOrOperation ?: ClientSessionOptions | WithSessionCallback < T > ,
650
+ callback ?: WithSessionCallback < T >
651
+ ) : Promise < T > {
652
652
const options = {
653
653
// Always define an owner
654
654
owner : Symbol ( ) ,
@@ -666,15 +666,17 @@ export class MongoClient extends TypedEventEmitter<MongoClientEvents> {
666
666
const session = this . startSession ( options ) ;
667
667
668
668
return maybeCallback ( async ( ) => {
669
+ let value ;
669
670
try {
670
- await withSessionCallback ( session ) ;
671
+ value = await withSessionCallback ( session ) ;
671
672
} finally {
672
673
try {
673
674
await session . endSession ( ) ;
674
675
} catch {
675
676
// We are not concerned with errors from endSession()
676
677
}
677
678
}
679
+ return value ;
678
680
} , null ) ;
679
681
}
680
682
0 commit comments