New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(connection): openUri return promise-like on existing connection #9358
Conversation
@@ -669,6 +669,12 @@ Connection.prototype.openUri = function(uri, options, callback) { | |||
if (typeof callback === 'function') { | |||
callback(null, this); | |||
} | |||
|
|||
this.then = (resolve) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this.then = (resolve) => { | |
this.then = (resolve) => { | |
delete this.then; | |
delete this.catch; | |
resolve(this); | |
return this | |
} |
if you don't do this, if you try to call Promise.resolve(conn)
or await conn
your interpreter will go in an infinite loop of calling resolve
recursively as it tries to flatten the promise, see #8810
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
makes, sense, fixed! However in this way, it's not possible to do conn.openUri(...).then().catch()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess if you really wanted to you could do
this.then = (resolve) => {
process.nextTick(() => {
delete this.then;
delete this.catch;
resolve(this);
});
return this;
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for this PR, but we're going to make an alternative fix that's more in line with where we want to go with #8810. Since mongoose.connect()
always returns a promise rather than something that can be used as a connection, we can change mongoose.connect()
instead of openUri()
.
Summary
Fixes #9335