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.

Why there's no await when calling find methods?

See original GitHub issue

(It’s not an issue, just a question)

Why there’s no await when calling find methods?

for example:

const getUserById = async (id) => {
  return User.findById(id);         <-- no await
};

or:

const getUserByEmail = async (email) => {
  return User.findOne({ email });   <-- no await
};

I thought that maybe the rationale was: “no await before mongoose methods”, but it can’t be that, because there is an await before other mongoose methods, such as create, for example:

const createUser = async (userBody) => {
  if (await User.isEmailTaken(userBody.email)) {
    throw new ApiError(httpStatus.BAD_REQUEST, 'Email already taken');
  }
  const user = await User.create(userBody);    <-- has await
  return user;
};

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5

github_iconTop GitHub Comments

2reactions
trasherdkcommented, May 26, 2021

@turanibrahim That will work too 😃 but consult link referenced in @Sicria’s reply.

1reaction
OfirD1commented, Jul 4, 2021

It’s unfortunate that this issue was closed, as my question wasn’t really answered. The answers here justify what happens in getUserById (which has no await), but then - by the same rationale - shouldn’t the await in createUser (which has an await) be removed?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Because this call is not awaited, execution of the current ...
Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the Await operator ...
Read more >
Finding Async Method Calls Missing Await - .NET Core Tutorials
Consider applying the 'await' operator to the result of the call. The problem with this is that the warning isn't always immediately noticeable....
Read more >
Is possible to call async function without await keyword? and ...
Call an async function without await and you will get a promise as the return value. Code following the await will execute immediately....
Read more >
Calling .NET Methods With and Without Async
Using the await keyword launches the method (and any code that follows it in the calling method) on a separate thread. When the...
Read more >
no-return-await - ESLint - Pluggable JavaScript Linter
Using return await inside an async function keeps the current function in the call stack until the Promise that is being awaited has...
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