Skip to content

Mongoose Query resolves even if it wasn't awaited #12172

Answered by vkarpov15
samislam asked this question in Q&A
Discussion options

You must be logged in to vote

If you return something that has a then() function from an async function, JavaScript will automatically call then(). There's no way to return a thenable from an async function.

You would have to wrap your query in an object, like:

async function x(y) {
  // @param y: function | asyncfunction
  const YreturnValue = await y()
  YreturnValue.sort('age')
  return { query: YreturnValue } // returns a Mongoose Query
}

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by samislam
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants