Automattic / mongoose

MongoDB object modeling designed to work in an asynchronous environment.
https://mongoosejs.com
MIT License
27.01k stars 3.85k forks source link

Returning mongoose Query object from within a promise #4033

Closed azhang closed 8 years ago

azhang commented 8 years ago

I'd like to return a query object from a promise (so I can reuse it with different chained methods), but since the query object is a thenable, it'll always resolve before I get there. Any way to work around this?

vkarpov15 commented 8 years ago

You can always just wrap the query in an object, like return { q: query }

azhang commented 8 years ago

What was the reasoning for putting .exec() thenable functionality into the Query itself? It seems redundant and confusing to make Query a thenable and have .exec(), when both behave the same when used with promises.

vkarpov15 commented 8 years ago

The reason was so you could do yield MyModel.findOne() when using co, without needing the extra .exec(), because that's a much more common use case than returning a query from a promise AFAIK.

Also, they don't behave exactly the same. You can't .catch() on a query, for instance.