Reject a promise from ()

How can you drop a promise from within then()

?

For example:

Promise.all(promiseArr).then(()=>{
  if(cond){
    //reject
  }
}).catch(()=>{ /*do something*/ });

      

The only relevant question I found was this: How to reject a promise from inside and then a function , but this is from 2014, so there must be a better way to then quit by now with ES6 support.

+3


source to share


1 answer


ES6 / ES2015 is still JavaScript and doesn't offer anything new about rejecting promises. In fact, native promises are ES6.

This is either

promise
.then(() => {
  return Promise.reject(...);
})
.catch(...);

      

or



promise
.then(() => {
  throw ...;
})
.catch(...);

      

And a throw

more idiomatic (and usually more efficient) way to do it.

This may not be true for other promise implementations. For example. in AngularJS throw

and $q.reject()

are not the same thing.

+4


source







All Articles