1

General Question

In JavaScript, I have a pending Promise object and want to reject it, before it is resolved.

I could use deferred.reject(), but it's marked as obsolete and seems to be an anti pattern.

So how to do it instead then? Are Promises the way to handle this at all?

More Specific

click1 = getPage1Promise();

// I want to reject click1 here, to avoid race conditions
click2 = getPage2Promise();

click1
    .then(page1 => console.log('show page 1'))
    .catch(err => console.log('dont show 1'))

click2
    .then(page2 => console.log('show page 2'))
    .catch(err => console.log('dont show 2'))
Waog
  • 7,127
  • 5
  • 25
  • 37
  • the code is so basic that any answer wont help with real world code – Jaromanda X Apr 20 '17 at 00:53
  • If you only have a reference to a standard ES6 promise object, then you cannot reject it from the outside. It is purposely designed that way. If you want to modify your own code to allow some outside agent to reject it, then you have to capture the reject callback and make it available to the outside. Your code in your question does not show us what you are really trying to do. Deferred objects (which some implementations have such as jQuery) can be rejected or resolved directly from the object. You can make your own Deferred object from a standard ES6 object and return that. – jfriend00 Apr 20 '17 at 00:59
  • FYI, here's how you can make your own Deferred in ES6 http://stackoverflow.com/a/37673534/816620, but usually there are better ways to write your code that make it so this is not necessary. – jfriend00 Apr 20 '17 at 01:17
  • I used to have some very complex code that I thought had to have the "Deferred" (anti) pattern - it took a while, but I managed to refactor (a lot of) code and now don't need no steenking deferred's :p – Jaromanda X Apr 21 '17 at 04:37

0 Answers0