Skip to content
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

Closed
wants to merge 4 commits into from

Conversation

DiegoRBaquero
Copy link

Summary
Fixes #9335

@@ -669,6 +669,12 @@ Connection.prototype.openUri = function(uri, options, callback) {
if (typeof callback === 'function') {
callback(null, this);
}

this.then = (resolve) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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

Copy link
Author

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()

Copy link
Contributor

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;
}

@vkarpov15 vkarpov15 closed this in fadc813 Aug 26, 2020
Copy link
Collaborator

@vkarpov15 vkarpov15 left a 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().

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

TypeError: conn.openUri(...).then is not a function
3 participants