deno.land / x / mongoose@6.7.5 / docs / promises.md
Mongoose async operations, like .save()
and queries, return thenables.
This means that you can do things like MyModel.findOne({}).then()
and
await MyModel.findOne({}).exec()
if you're using
async/await.
You can find the return type of specific operations in the api docs You can also read more about promises in Mongoose.
[require:Built-in Promises]
Mongoose queries are not promises. They have a .then()
function for co and async/await as
a convenience. If you need
a fully-fledged promise, use the .exec()
function.
[require:Queries are not promises]
Although queries are not promises, queries are thenables.
That means they have a .then()
function, so you can use queries as promises with either
promise chaining or async await
[require:Queries are thenable]
exec()
With await
?There are two alternatives for using await
with queries:
await Band.findOne();
await Band.findOne().exec();
As far as functionality is concerned, these two are equivalent.
However, we recommend using .exec()
because that gives you
better stack traces.
[require:Should You Use `exec\(\)` With `await`]
If you're an advanced user, you may want to plug in your own promise
library like bluebird. Just set
mongoose.Promise
to your favorite
ES6-style promise constructor and mongoose will use it.
[require:Plugging in your own Promises Library]
Version Info