I have a function in a chain of promises that may or may not do something. E.g.
getYear().then(function(results){
if(results.is1999) return party();
else return Q.fcall(function(){/*do nothing here*/});
}).then(sleep)
Where getYear
, party
, and sleep
all return promises. Is there a more concise way to write the else statement? That is, do nothing, but still return a chainable promise?
An "empty promise" is another way of saying "a lie" in english.. so having an instantly resolved promise is better, just from a language standpoint ;) Promise. resolve() worked for me! new Promise now requires a parameter, use Promise.
Empty-promise definition (idiomatic) A promise that is either not going to be carried out, worthless or meaningless. noun.
Yes. Q(value)
returns a promise for the value
(it also unwraps the value if value
is a promise).
getYear().then(function(results){
if(results.is1999) return party();
else return Q(undefined);
}).then(sleep)
Alternatively, you can get the exact same semantics by just not returning anything:
getYear().then(function(results){
if(results.is1999) return party();
}).then(sleep)
If what you wanted was a promise that's never resolved, your best bet would be
getYear().then(function(results){
if(results.is1999) return party();
else return Q.promise(function () {});
}).then(sleep)
What you could do is re-use the same promise:
var stop = Q.promise(function () {});
getYear().then(function(results){
if(results.is1999) return party();
else return stop
}).then(sleep)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With