Skip to content

re: Modern JavaScript, 10 things you should be using, starting today VIEW POST

re: Thanks. I'd love to see how handling async/await errors can be done in a way that isn't just as complicated as then/catch

you can do

shop.getFreeKittens().then(kittens=>console.log(kittens), err=>{
  // do your thing

yes, definitely that too. In my example that would be a catch-all handler. question I think was how to handle per call . So in promises it would be:

  .then(getOrderByUser, errorHandler)
  .then(getOrderItemsByOrder, errorHandler)

The above scenario is what I think we are discussing.

For an error handling approach for async/await, we could do this:

async function get() {
  let user, order;
  try {
    user = await getUser();
  } catch(err) {}

 try {
  order = await getOrderByUser(user);
 } catch(err) {


  .then(successHandler, errorHandler)

I get the feeling that people can feel a bit intimidated when they first see a longer promise chain. this is were I believe async/await come in to their own, When used properly async functions can look pretty simple and straight forward. I still prefer promise style functions for most async tasks tho. Especially task like my example above.

code of conduct - report abuse