As an example:
if(foo) { async_call(); // returns a Q promise object } // do this immediately if foo was false // or after the promise is resolved if foo was true
Is it possible?
The alternatives that I see either always call the async function, throwing a "foo" check, which is less optimal, but will work inside the code; or just assuming that "foo" is true and throws an error if it is false (requiring the caller to try again after calling the async function itself), which is even less ideal for obvious reasons.
EDIT: as a temporary work, I found this to work, but this is hardly ideal:
var promise = foo ? Q.defer().resolve(foo).promise : async_call(); promise.then(function(foo) { // this is done after the async call if it was necessary or immediately if not });
source share