File tree Expand file tree Collapse file tree 1 file changed +38
-1
lines changed Expand file tree Collapse file tree 1 file changed +38
-1
lines changed Original file line number Diff line number Diff line change @@ -693,6 +693,42 @@ function* makeCounter(seed) {
693
693
}
694
694
}
695
695
696
+ /**
697
+ * Helper function for either accepting a callback, or returning a promise
698
+ *
699
+ * @param {Function } [callback] an optional callback.
700
+ * @param {Function } fn A function that takes a callback
701
+ * @returns {Promise|void } Returns nothing if a callback is supplied, else returns a Promise.
702
+ */
703
+ function maybePromise ( callback , fn ) {
704
+ let result ;
705
+ if ( typeof callback !== 'function' ) {
706
+ result = new Promise ( ( resolve , reject ) => {
707
+ callback = ( err , res ) => {
708
+ if ( err ) return reject ( err ) ;
709
+ resolve ( res ) ;
710
+ } ;
711
+ } ) ;
712
+ }
713
+
714
+ fn ( function ( err , res ) {
715
+ if ( err != null ) {
716
+ try {
717
+ callback ( err ) ;
718
+ } catch ( error ) {
719
+ return process . nextTick ( ( ) => {
720
+ throw error ;
721
+ } ) ;
722
+ }
723
+ return ;
724
+ }
725
+
726
+ callback ( err , res ) ;
727
+ } ) ;
728
+
729
+ return result ;
730
+ }
731
+
696
732
module . exports = {
697
733
filterOptions,
698
734
mergeOptions,
@@ -722,5 +758,6 @@ module.exports = {
722
758
MongoDBNamespace,
723
759
resolveReadPreference,
724
760
emitDeprecationWarning,
725
- makeCounter
761
+ makeCounter,
762
+ maybePromise
726
763
} ;
You can’t perform that action at this time.
0 commit comments