Hacker News new | past | comments | ask | show | jobs | submit login

Promises catch exceptions and automatically reject: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe... . I've never seen a promise not do this - do you have an example where this is not the case?



As I wrote in depends whether you can survive and continue from an exception inside the .then to the next step or not -- not about how the promise would handle an exception, but whether you want it to handle it or you want to survive and handle it yourself.

e.g, naive example:

  .then(() => {
     var foo;
     try {
          foo = getFoo();
     } catch (err) {
          foo = 10; //some default value
     }
     return foo;
  }).then( (foo) => 
etc.

So you might not want to reject automatically inside then if getFoo raises, but recover and return a default value.


That example could be written:

    .then(getFoo)
    .catch(err => 10) //some default value
    .then( (foo) =>


cool, learned something new




Consider applying for YC's Summer 2025 batch! Applications are open till May 13

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: