12

Here's some code:

  import 'babel-polyfill'

  async function helloWorld () {
    throw new Error ('hi')
  }

  helloWorld()

I also went deep and tried this as well:

  import 'babel-polyfill'

  async function helloWorld () {
    throw new Error ('hi')
  }

  async function main () {
    try {
      await helloWorld()
    } catch (e) {
      throw e
    }
  }

  main()

and:

import 'babel-polyfill'

 async function helloWorld () {
   throw new Error ('hi')
 }

try {
 helloWorld()
} catch (e) {
 throw e
}

This works:

import 'babel-polyfill'

async function helloWorld () {
  throw new Error('xxx')
}

helloWorld()
.catch(console.log.bind(console))
ThomasReggi
  • 55,053
  • 85
  • 237
  • 424

3 Answers3

7

async is meant to be used with Promises. If you reject the promise, then you can catch the error, if you resolve the promise, that becomes the return value of the function.

async function helloWorld () {
  return new Promise(function(resolve, reject){
    reject('error')
  });
}


try {
    await helloWorld();
} catch (e) {
    console.log('Error occurred', e);
}
Ruan Mendes
  • 90,375
  • 31
  • 153
  • 217
  • 1
    I just found you can throw like normal and just use `.catch` (show above). You're method doesn't work for me. – ThomasReggi Nov 06 '15 at 15:03
  • @ThomasReggi [This link claims it should work](http://www.sitepoint.com/simplifying-asynchronous-coding-es7-async-functions/) (and also throwing exceptions) Maybe it's a bug in babel? Yes the `.catch()` will work, but I thought your question was about being able to `try(){}catch(){}` it – Ruan Mendes Nov 06 '15 at 15:26
  • AFAIK, you can only use the await keyword within an aync function. – Jehan Jun 08 '16 at 22:03
  • @jehan it comes before a function to signify you can call it with await . https://jakearchibald.com/2014/es7-async-functions/ – Ruan Mendes Jun 09 '16 at 11:48
  • Works for me with axios promises. – luckydonald Nov 04 '21 at 14:50
5

So it's kind of tricky, but the reason you're not catching the error is because, at the top level, the entire script can be thought of as a synchronous function. Anything you want to catch asynchronously needs to be wrapped in an async function or using Promises.

So for instance, this will swallow errors:

async function doIt() {
  throw new Error('fail');
}

doIt();

Because it's the same as this:

function doIt() {
  return Promise.resolve().then(function () {
    throw new Error('fail');
  });
}

doIt();

At the top level, you should always add a normal Promise-style catch() to make sure that your errors get handled:

async function doIt() {
  throw new Error('fail');
}

doIt().catch(console.error.bind(console));

In Node, there is also the global unhandledRejection event on process that you can use to catch all Promise errors.

nlawson
  • 11,510
  • 4
  • 40
  • 50
0

To catch an error from an async function, you can await the error:

async function helloWorld () {
  //THROW AN ERROR FROM AN ASYNC FUNCTION
  throw new Error('hi')
}

async function main() {
  try {
    await helloWorld()
  } catch(e) {
    //AWAIT THE ERROR WITHIN AN ASYNC FUNCTION
    const error = await e
    console.log(error)
  }
}

main()

Alternatively, you can just await the error message:

async function main() {
  try {
    await helloWorld()
  } catch(e) {
    //AWAIT JUST THE ERROR MESSAGE
    const message = await e.message
    console.log(message)
  }
}
Rolazar
  • 120
  • 1
  • 8