Mongoose - Why we make “mongoose.Promise = global.Promise” when setting a mongoose module?

前端 未结 5 1125
野性不改
野性不改 2020-12-29 18:04

I\'m working with Mongoose. I have seen a lot of developers make the following command:

mongoose.Promise = global.Promise;

Then I was curi

5条回答
  •  臣服心动
    2020-12-29 18:45

    This is legacy code from older examples that isn't needed with Mongoose 5.

    Mongoose 4 relied on its own promise implementation, mpromise. mongoose.Promise wasn't necessarily Promise global.

    As Mongoose 4 documentation states:

    Mongoose 5.0 will use native promises by default (or bluebird, if native promises are not present) but still support plugging in your own ES6-compatible promises library. Mongoose 5.0 will not support mpromise.

    Though the statement about Bluebird is no longer true; Mongoose 5 dropped the support of Node versions that don't have native promises.

    mongoose.Promise = global.Promise;
    

    may still be needed if global.Promise was assigned with another implementation (e.g. Bluebird) after Mongoose was imported, though a better thing would be to assign global.Promise = Bluebird earlier instead.

提交回复
热议问题