question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Rejecting promises

See original GitHub issue

How do you reject promises inside the then handlers?

Restangular.all('resource').get().then(function(){

//how to reject it?

}, function(){

//how to reject it?

});

Do you just construct the rejection using $q service? I thought there might be something inside Restangular I would be using.

$q.reject();

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
kellynicholscommented, Mar 14, 2014

Below is an example of returning the rejection from the then success handler. I personally prefer to use the catch handler to handle the errors.

Restangular.all('resource').get()
  .then(function(resource) {
    return $q.reject(new Error("This resource is bad."));
  })
  .catch(function(error) {
    console.log("error", error.message);
  });
0reactions
grabboucommented, Feb 24, 2015

It just means that catch is not final and you chain them in the way you like.

$promise.then(function downloadFromServer() {

}).catch(function handleServerError(e) {

}).then(function parseServerResponse() {
// this will run if no errors above, otherwise will be skipped
});

With bluebird for example, you can chain multiple catch statements and pass error class in order to catch only the ones you are capable of handling there.
Read more comments on GitHub >

github_iconTop Results From Across the Web

Promise.reject() - JavaScript - MDN Web Docs
The Promise.reject() method returns a Promise object that is rejected with a given reason.
Read more >
JavaScript promise reject() Method - GeeksforGeeks
The Promise.reject() method is used to return a rejected Promise object with a given reason for rejection. It is used for debugging purposes ......
Read more >
JavaScript Promises - reject vs. throw - Stack Overflow
If it returns a rejected promise, the promise returned by the call to then is resolved to that promise (and will ultimately be...
Read more >
JavaScript Promise Tutorial – How to Resolve or Reject ...
A Promise executor should call only one resolve or one reject . Once one state is changed (pending => fulfilled or pending =>...
Read more >
Reject a Promise in JavaScript - Mastering JS
The Promise.reject() function is the most concise way to create a rejected promise that contains a given error. You should then use .catch() ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found