Crhistian
Crhistian

Reputation: 1282

Conditional promise chaining?

I have a situation where depending upon a variable one of two situations can occur. Both of these situations return a promise and have the exact same logic applied afterwards.

I want something like this to happen:

userisBuyer ? Order.Create : Order.Create.As.Buyer
    .then //do same stuff for both

I realize this isn't valid logic but how could I accomplish something similar where I .then on both of those conditionally?

Upvotes: 0

Views: 462

Answers (2)

James Thorpe
James Thorpe

Reputation: 32212

You can do this in one line, as implied at by your question format:

(userisBuyer ? Order.Create() : Order.Create.As.Buyer()).then(function() {
     //do same stuff for both
});

But do you really want to?

var createMethod = userisBuyer ? Order.Create : Order.Create.As.Buyer;
createMethod().then(function() {

});

is much more readable in my eyes.

Upvotes: 6

Sami Triki
Sami Triki

Reputation: 408

let action = userisBuyer ? Order.Create : Order.Create.As.Buyer;

action()
.then(res => {your code})

This should do ?

Upvotes: 2

Related Questions