Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

mongoose.Promise = global.Promise;

Then I was curious to see what is the original value of mongoose.Promise . I have entered in my editor the following command:

const mongoose = require("mongoose");

console.log("promise: ", mongoose.Promise);

My console returned me :

promise: function Promise() { [native code] }

Okay, so why make the command mongoose.Promise = global.Promise since the Mongoose's promise already returns a native code ? I don't understand the point, if someone can help us to understand, would be great,

Thanks

like image 285
Webwoman Avatar asked Aug 15 '18 16:08

Webwoman


People also ask

What does Mongoose Promise do?

Built-in Promises Mongoose async operations, like . save() and queries, return thenables. This means that you can do things like MyModel.

Does Mongoose save return a Promise?

While save() returns a promise, functions like Mongoose's find() return a Mongoose Query . Mongoose queries are thenables. In other words, queries have a then() function that behaves similarly to the Promise then() function. So you can use queries with promise chaining and async/await.

Is Mongoose find a Promise?

Mongoose queries are not promises. However, they do have a . then() function for yield and async/await.


2 Answers

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.

like image 131
Estus Flask Avatar answered Oct 06 '22 12:10

Estus Flask


Mongoose maintainer here. If you're using Mongoose 5, please remove mongoose.Promise = global.Promise;. That line was used to address the below deprecation warning with promises in Mongoose 4:

WARNING: Mongoose: mpromise (mongoose's default promise library) is deprecated, plug in your own promise library instead

It does nothing in Mongoose 5. You should only use mongoose.Promise in Mongoose 5 if you want to use your own promise library with Mongoose, like Bluebird or Q.

There's also a little more about mongoose.Promise here: https://masteringjs.io/tutorials/mongoose/promise#the-mongoosepromise-property

like image 28
vkarpov15 Avatar answered Oct 06 '22 11:10

vkarpov15