DEV Community

Discussion on: What's new in ES2022? ๐Ÿค”

 
jasmin profile image
Jasmin Virdi

Thanks for attaching this link for reference. ๐Ÿ™‚

The way this would be super helpful is in deeply nested functions with chained errors.
Like if we have mutiple try catch block inside a deeply nested function so we pass the string as the purpose of that block and cause as the argument of catch block. For example:

function deeplyNested() {
  try {
    // outer block logic.

    let appData = [{...}]
    // app data array parsing.
    appData.map(
      (data) => {
        try {
          // ยทยทยท
        } catch (error) {
          throw new Error(`While processing ${data}`, {
            cause: error
          });
        }
      });
  } catch (error) {
    throw Error('Error while processing outer block', {
      cause: error
    });
  }
}

Enter fullscreen mode Exit fullscreen mode