Docs falsely suggest `exec()` for async/await
See original GitHub issueDo you want to request a feature or report a bug?
Docs.
What is the current docs?
https://mongoosejs.com/docs/promises.html#built-in-promises
This means that you can do things like MyModel.findOne({}).then() and await MyModel.findOne({}).exec() if you’re using async/await.
What is the expected docs?
await Model.findOne()
works just fine.
This means that you can do things like
MyModel.findOne({}).then()
andawait MyModel.findOne({})
if you’re using async/await.
What are the versions of Node.js, Mongoose and MongoDB you are using? Note that “latest” is not a version.
Mongoose 5.5.14 Node.js 10.16.2
Issue Analytics
- State:
- Created 4 years ago
- Reactions:2
- Comments:13
Top Results From Across the Web
Async/Await - Best Practices in Asynchronous Programming
Configure context, Use ConfigureAwait(false) when you can ... Sleep(1000); } async Task MyMethodAsync() { // Do asynchronous work. await Task.Delay(1000); }.
Read more >async function - JavaScript - MDN Web Docs - Mozilla
The async function declaration declares an async function where the await keyword is permitted within the function body. The async and await ......
Read more >SyntaxError: Unexpected token function - Async Await Nodejs
I have no clue why, the terminal throws up an error when I try to execute the node code. helloz.js (async function testingAsyncAwait()...
Read more >Coroutines and Tasks — Python 3.11.1 documentation
Coroutines declared with the async/await syntax is the preferred way of writing ... The asyncio.create_task() function to run coroutines concurrently as ...
Read more >Async and Await - Stephen Cleary
Later on, when the awaitable completes, it will execute the remainder of the async method. If you're awaiting a built-in awaitable (such as ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@Moeriki The difference is that the return type of
Model.findOne
is aQuery
. They just happen to have added athen
to it so it is “thennable” but it is not a true promise. The return fromexec
is actually a promise if you don’t supply a callback to theexec
. While you might not care about this distinction some of us do.You’re right that queries are not promises: https://mongoosejs.com/docs/queries.html#queries-are-not-promises . The most meaningful difference is that if you
await
on the same query object twice, you will execute the query twice, whereasawait
on the same promise twice only executes the query once. We did also run into an issue where node’s async_hooks don’t work properly if youawait
on a custom thennable, but that’s a node issue that mongoose can’t work around.TLDR; there are reasons to prefer using
exec()
, but for most apps it doesn’t matter which one.@jloveridge I’m curious to hear why you prefer a true promise over a thennable. Is it one of the reasons I listed or something else?