Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect explanation of catching errors with asynchronous JavaScript in Express Tutorial #37719

Open
granteckels opened this issue Jan 20, 2025 · 5 comments
Labels
Content:Learn:Express Learning area Express docs

Comments

@granteckels
Copy link

MDN URL

https://developer.mozilla.org/en-US/docs/Learn_web_development/Extensions/Server-side/Express_Nodejs/mongoose

What specific section or headline is this issue about?

Database APIs are asynchronous

What information was incorrect, unhelpful, or incomplete?

In this section, in the part where it explains how to catch errors from async functions, I believe it's wrong. It puts the try...catch block on the async function call instead of inside the function or by running catch() on the async function call. In the current code, the error will not be caught.

This is the code in question:

async function myFunction {
  // ...
  await someObject.methodThatReturnsPromise();
  // ...
  await aFunctionThatReturnsPromise();
  // ...
}

try {
  // ...
  myFunction();
  // ...
} catch (e) {
 // error handling code
}

What did you expect to see?

Something like this with a proper explanation:

async function myFunction {
  // ...
  try {
    await someObject.methodThatReturnsPromise();
  } catch(e) {
    // error handling code
  }
  // ...
  try {
    await aFunctionThatReturnsPromise();
  } catch(e) {
    // error handling code
  }
  // ...
}

myFunction();

or

async function myFunction {
  // ...
  await someObject.methodThatReturnsPromise();
  // ...
  await aFunctionThatReturnsPromise();
  // ...
}

myFunction().catch((e) => {
  // error handle error
});

Do you have any supporting links, references, or citations?

No response

Do you have anything more you want to share?

No response

MDN metadata

No response

@granteckels granteckels added the needs triage Triage needed by staff and/or partners. Automatically applied when an issue is opened. label Jan 20, 2025
@hamishwillee
Copy link
Collaborator

Have you tried it?

My understanding is that as long as the potentially throwing function(s) are awaited, which they are, then this construction should work. So would your first example. Not sure about the second.

@Josh-Cena Can you advise whether the current code is OK? I'm pretty sure I tested this, but perhaps not.

@Josh-Cena
Copy link
Member

async function myFunction {
  // ...
  await someObject.methodThatReturnsPromise();
  // ...
  await aFunctionThatReturnsPromise();
  // ...
}

try {
  // ...
  myFunction();
  // ...
} catch (e) {
 // error handling code
}

Indeed, this would never catch anything from myFunction, because it never synchronously throws an error. Both of your suggestions look good to me.

@Josh-Cena Josh-Cena added Content:Learn:Express Learning area Express docs and removed needs triage Triage needed by staff and/or partners. Automatically applied when an issue is opened. labels Jan 20, 2025
@hamishwillee
Copy link
Collaborator

Thanks both. I had figured that since someObject.methodThatReturnsPromise() awaits in the myFunction that effectively made myFunction synchronous w.r.t. the try. I just don't get this stuff.

Anyhow, I guess we should fix https://github.com/mdn/express-locallibrary-tutorial first and then update the docs. @granteckels Did you want to do the updates?

@granteckels
Copy link
Author

The actual repo doesn't contain this code. It's just used as an example when explaining async JavaScript in the docs. So only the docs need to be updated. And, yes, I can do this.

@hamishwillee
Copy link
Collaborator

Thank you very much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Content:Learn:Express Learning area Express docs
Projects
None yet
Development

No branches or pull requests

3 participants