question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Docs falsely suggest `exec()` for async/await

See original GitHub issue

Do 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() and await 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:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:13

github_iconTop GitHub Comments

3reactions
jloveridgecommented, Sep 6, 2019

@Moeriki The difference is that the return type of Model.findOne is a Query. They just happen to have added a then to it so it is “thennable” but it is not a true promise. The return from exec is actually a promise if you don’t supply a callback to the exec. While you might not care about this distinction some of us do.

1reaction
vkarpov15commented, Sep 10, 2019

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, whereas await 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 you await 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?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found