@@ -273,7 +273,7 @@ export interface MongoClientOptions extends BSONSerializeOptions, SupportedNodeC
273273}
274274
275275/** @public */
276- export type WithSessionCallback = ( session : ClientSession ) => Promise < any > ;
276+ export type WithSessionCallback < T = any > = ( session : ClientSession ) => Promise < T > ;
277277
278278/** @internal */
279279export interface MongoClientPrivate {
@@ -643,12 +643,12 @@ export class MongoClient extends TypedEventEmitter<MongoClientEvents> {
643643 * @param options - Optional settings for the command
644644 * @param callback - An callback to execute with an implicitly created session
645645 */
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 > {
652652 const options = {
653653 // Always define an owner
654654 owner : Symbol ( ) ,
@@ -666,15 +666,17 @@ export class MongoClient extends TypedEventEmitter<MongoClientEvents> {
666666 const session = this . startSession ( options ) ;
667667
668668 return maybeCallback ( async ( ) => {
669+ let value ;
669670 try {
670- await withSessionCallback ( session ) ;
671+ value = await withSessionCallback ( session ) ;
671672 } finally {
672673 try {
673674 await session . endSession ( ) ;
674675 } catch {
675676 // We are not concerned with errors from endSession()
676677 }
677678 }
679+ return value ;
678680 } , null ) ;
679681 }
680682
0 commit comments