Skip to content

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

@granteckels

Description

@granteckels

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions