Skip to content

Commit 3acfb6d

Browse files
committed
fix: withSession
1 parent c232a19 commit 3acfb6d

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

src/mongo_client.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -642,16 +642,14 @@ export class MongoClient extends TypedEventEmitter<MongoClientEvents> {
642642
}
643643

644644
const session = this.startSession(options);
645-
const PromiseConstructor = PromiseProvider.get() ?? Promise;
646-
647-
return PromiseConstructor.resolve()
648-
.then(() => withSessionCallback(session))
649-
.then(() => {
650-
// Do not return the result of callback
651-
})
652-
.finally(() => {
645+
646+
return maybeCallback(async () => {
647+
try {
648+
await withSessionCallback(session);
649+
} finally {
653650
session.endSession().catch(() => null);
654-
});
651+
}
652+
}, null);
655653
}
656654

657655
/**

src/utils.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,9 +438,14 @@ export function* makeCounter(seed = 0): Generator<number> {
438438
/**
439439
* Helper for handling legacy callback support.
440440
*/
441+
export function maybeCallback<T>(promiseFn: () => Promise<T>, callback: null): Promise<T>;
441442
export function maybeCallback<T>(
442443
promiseFn: () => Promise<T>,
443444
callback?: Callback<T>
445+
): Promise<T> | void;
446+
export function maybeCallback<T>(
447+
promiseFn: () => Promise<T>,
448+
callback?: Callback<T> | null
444449
): Promise<T> | void {
445450
const PromiseConstructor = PromiseProvider.get();
446451

0 commit comments

Comments
 (0)