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?
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)