Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
56
rated 0 times [  58] [ 2]  / answers: 1 / hits: 35724  / 6 Years ago, wed, august 15, 2018, 12:00:00

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


More From » node.js

 Answers
108

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.


[#53727] Friday, August 10, 2018, 6 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
stacied

Total Points: 124
Total Questions: 84
Total Answers: 98

Location: Ivory Coast
Member since Sun, Mar 7, 2021
3 Years ago
;