Creating a Mongoose plugin that supports callbacks AND promises Creating a Mongoose plugin that supports callbacks AND promises mongoose mongoose

Creating a Mongoose plugin that supports callbacks AND promises


just do this

mongoose.Promise = global.Promise

as simple as that


So I mainly didn't want to rely on a specific promise library, incase they were using a different one, but I realized that for the most part, it shouldnt really matter. So I decided to stick with Bluebird, and use the asCallback method

Heres the end result, this is with me coding a library with a function that supports both callbacks and promises; and in a separate file, and without requiring a specific promise library, using that function twice, once with a promise, and once with a callback:

// ./test-lib.js'use strict'const Promise = require( 'bluebird' )module.exports = ( str, cb ) => {    const endResult = ( txt ) => new Promise( ( res, rej ) => res( 'You said ' + txt ) )    return endResult( str ).asCallback( cb );}// app.js'use strict'const Testlib = require('./test-lib')Testlib('foo', ( err, data ) => {    if( err )        console.error('ERROR:',err)    else        console.log('DATA:',data)})Testlib('bar')    .then( data => {        console.log('DATA:',data)    })    .catch( err => {        console.error('ERROR:',err)    })// Result:// DATA: You said foo// DATA: You said bar

That seems to have worked perfectly! (Thanks to tyscorp the Bluebird Gitter chat)