Try not to create a single deferred, that could be rejected from multiple parts of your function, and which you would need to return at every exit point.
Instead, code with separate promises, one for each branch of control flow you have. You can use Q.reject
and the Q.Promise
constructor – avoid the deprecated deferred pattern. Your function would then look like this:
function test() {
var deferred = q.defer()
var passed = false
if (!passed)
return Q.reject("Don't proceed");
if (!passed)
return Q.reject("Don't proceed");
if (!passed)
return Q.reject("Don't proceed");
// else
return new Promise(function(resolve) {
setTimeout(function(){
console.log("Hello");
resolve();
}, 100);
});
}
Alternatively, you can wrap your test
function in Q.fbind
, so that instead of writing return Q.reject(…);
you could just do throw …;
.