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

Consistently return Promises from Model-Queries on .exec() #7307

Closed
mhombach opened this issue Dec 7, 2018 · 1 comment
Closed

Consistently return Promises from Model-Queries on .exec() #7307

mhombach opened this issue Dec 7, 2018 · 1 comment
Labels
discussion If you have any thoughts or comments on this issue, please share them!

Comments

@mhombach
Copy link

mhombach commented Dec 7, 2018

Do you want to request a feature or report a bug?
Some kind of feature, more a question/discussion

What is the current behavior?
Model.create() returns a Promise, while Model.find/otherQuery returns a DocumentQuery-Object.

What is the expected behavior?
The Model-Functions should maybe behave in the same way, so that Model.create() also returns a DocumentQuery/Query and only Model.create().exec() returns the Promise.
This is more a consistency thing i stumbled across at this moment and i am wondering if there is a good reason to do this the way it is right now :)

Please mention your node.js, mongoose and MongoDB version.
All latest versions

@vkarpov15
Copy link
Collaborator

Because Mongoose queries have a then() and catch(), there is theoretically no need to use exec(). You can use promise chaining, async/await, etc. as you would normally. Practically, there's going to be some edge cases like #7292, but those are few and far in between.

The reason why Model.find() returns a query instead of a promise is because Mongoose queries are chainable, so you can do things like Model.find().sort().limit().skip().

@vkarpov15 vkarpov15 added the discussion If you have any thoughts or comments on this issue, please share them! label Dec 13, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
discussion If you have any thoughts or comments on this issue, please share them!
Projects
None yet
Development

No branches or pull requests

2 participants