-3

I need to test the result from a promise. Will a promise resolve in time to be part of a conditional?

if (myPromise() > 15 && myOtherPromise()) {
    // do stuff when myPromise resolves to over 15 and myOtherPromise is true
} else {
    // do stuff when myPromise resolves to 15 or less or myOtherPromise is false
}

Or do I just need to suck it up and do:

let myP = myPromise();
let myOtherP = myOtherPromise();

promise.all([myP, myOtherP]).then(function(res) {
    if (res[0] > 15 && res[1]) {
        // do stuff when myPromise resolves to over 15 and myOtherPromise is true
    } else {
        // do stuff when myPromise resolves to 15 or less or myOtherPromise is false
    }
});

reposted from comment below for everyone "downvoting" the question and thinking this is a repeat.

Well, yes and no. No, because I'm working with Jasmine, where the expect calls automagically resolves a promise when one is passed. expect(myPromise()).toBeGreaterThan(15). So from the outside, it might have been conceivable that an if's conditional might resolve a promise before performing any comparisons.

Machtyn
  • 2,982
  • 6
  • 38
  • 64
  • You cannot know (That's the e point of asynchronous). Just "suck it up" or call a callback function – nicovank Nov 09 '16 at 18:27
  • Well, yes and no. No, because I'm working with Jasmine, where the expect calls automagically resolves a promise when one is passed. `expect(myPromise()).toBeGreaterThan(15)`. So from the outside, it *might* have been conceivable that an if's conditional might resolve a promise before performing any comparisons. – Machtyn Nov 09 '16 at 18:33
  • @Machtyn `if` and the other control flow constructs have no support for promises. It would be interesting to have async versions of them, but I don't know of any proposal for that. – ssube Nov 09 '16 at 18:35
  • How in the world is this a duplicate of what it was marked of. I don't see that AT all. This is a specific question about accessing the resolved value of a couple promises which is not at what that other question is about. – jfriend00 Nov 09 '16 at 18:41
  • @Machtyn Jasmine might be magic, but `if` is not. – Bergi Nov 10 '16 at 19:34

1 Answers1

0

Assuming myPromise and myOtherPromise are functions that return promises which your code seems to indicate, then NO you can't do:

if (myPromise() > 15 && myOtherPromise()) {

Promises are objects. So if myPromise() returns a promise, then:

if (myPromise())`

is always truthy and that if statement has absolutely nothing to do with the eventual resolved value of the promise. So, that first if statement will NOT be checking the resolved value of either promise.


If you want to see the resolved value of a promoise, you HAVE to use something like .then() or .all() to get the resolved value. There is no other way to access the resolved value.

If you want to examine both the resolved values of myPromise() and myOtherPromise(), then you must wait for both of them to resolve and you should use Promise.all() as in your second code example:

Promise.all([myPromise(), myOtherPromise()]).then(function(results) {
    if (results[0] > 15 && results[1]) {
        // got the result we wanted
    }
}).catch(function(err) {
    // error here
});
jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • Yeah, I should have realized that when I did myOtherPromise(), and instead should have done myOtherPromise() == true in my "bad" example. – Machtyn Nov 09 '16 at 18:47
  • @Machtyn - Huhhh? `myOtherPromise() == true` doesn't do you any good either. A promise is an object. You can ONLY get its value with something like `p.then()` or `Promise.all([p1, p2])`. You can NEVER get it's value with `if (p)`. – jfriend00 Nov 09 '16 at 18:50
  • @Machtyn - An `if` statement is just plain Javascript. Some testing framework does not and can not change how an `if` statement works. – jfriend00 Nov 09 '16 at 18:50
  • Right. That's why I said my "bad" example. – Machtyn Nov 09 '16 at 18:51
  • @Machtyn - So, does this answer your question? – jfriend00 Nov 09 '16 at 18:51
  • Yes. I have to wait a few minutes before I can mark it. – Machtyn Nov 09 '16 at 18:53